Harinya
Harinya

Reputation: 191

ArgumentError - wrong number of arguments (2 for 1) when testing show method of controller

I'm trying to write test for show method of controller.

Below is the method:

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

Here is the test i have so far:

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

This test returns the following error:

 Failure/Error: get :show, id: @goal.name
 ArgumentError:
   wrong number of arguments (2 for 1)

The error points to goal= Goal.find_by_name(params[:id], :include => :projects, :conditions => ['projects.flag = true OR projects.user_id = ?', current_user.id]) line.

I'm not sure how to go about with this. Any leads regarding this will be helpful.

Upvotes: 1

Views: 376

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

You should use up to date syntax, because I assume you're not using this really old 2.3 Rails version. I suggest something like this:

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])

Upvotes: 2

Related Questions