ArgumentError-测试控制器的show方法时,参数数目错误(2为1)

时间:2019-03-19 05:50:54

标签: ruby-on-rails rspec controller rspec-rails

我正在尝试为控制器的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])行。

我不确定该如何处理。 任何与此相关的线索都会有所帮助。

1 个答案:

答案 0 :(得分:2)

您应该使用最新的语法,因为我假设您没有使用这个真正的旧2.3 Rails版本。我建议这样:

M17:N17