我正在尝试为控制器的show方法编写测试。
以下是方法:
def show
if current_user
goal= Goal.find_by_name(params[:id], :include => :projects, :conditions => ['projects.flag = true OR projects.user_id = ?', current_user.id])
else
goal= Goal.find_by_name(params[:id], :include => :projects, :conditions => ['projects.flag = true'])
end
@results = goal.projects if goal
end
这是我到目前为止的测试:
describe '#show' do
before :each do
@user = FactoryGirl.create(:user)
@project= FactoryGirl.create(:project, user: @user, flag: 1)
@goal= FactoryGirl.create(:goal, name: 'goal')
@goal_association= FactoryGirl.create(:goal_association, goal_id: @goal.id, project_id: @project.id)
controller.stub(:current_user).and_return(@user)
end
it 'search by goal' do
get :show, id: @goal.name
expect(response.status).to eq 302
end
end
此测试返回以下错误:
Failure/Error: get :show, id: @goal.name
ArgumentError:
wrong number of arguments (2 for 1)
错误指向goal= Goal.find_by_name(params[:id], :include => :projects, :conditions => ['projects.flag = true OR projects.user_id = ?', current_user.id])
线。
我不知道如何解决这个问题。任何关于此的线索都会有所帮助。
你应该使用最新的语法,因为我假设你没有使用这个真正老的2.3 Rails版本。我建议像这样:
scope = if current_user
Goal.joins(:projects).where('projects.flag = true OR projects.user_id = ?', current_user.id)
else
Goal.joins(:projects).where(projects: { flag: true })
end
goal = scope.find_by(name: params[:id])