Arun Sharma
Arun Sharma

Reputation: 281

how to solve nil error in rspec

I have a problem on nil error

My Controller code is:

def list
  @user=User.find(params[:id])
end

My Rspec code is:

it 'should_test_list'
  @user_mock=mock(User)
  User.should_receive(:find).and_return(@user)
  get :list,:id=>1
  assigns[:user].should==''
end

I am getting an error that expected ='' got nil

I want to ask that how to solve this error??

I am waiting for reply

Upvotes: 0

Views: 255

Answers (1)

David Chelimsky
David Chelimsky

Reputation: 9000

The RSpec example declares @user_mock, but then the next line uses @user instead. Needs to be:

User.should_receive(:find).and_return(@user_mock)

Upvotes: 3

Related Questions