I have a controller spec and I get following failed expectation:
Failure/Error: put :update, :id => login_user.id, :user => valid_attributes
#<User:0xbd030bc> received :update_attributes with unexpected arguments
expected: ({:name=>"changed name", :email=>"changed@mail.com", :password=>"secret", :password_confirmation=>"secret"})
got: ({"name"=>"Test user", "email"=>"user@test.com", "password"=>"secret", "password_confirmation"=>"secret"})
And for me it looks like I am passing in "name" => "Test User" and I am expecting :name => "test user"
my spec looks like this:
describe 'with valid parameters' do
it 'updates the user' do
login_user = User.create!(valid_attributes)
controller.stub(:current_user).and_return(login_user)
User.any_instance.
should_receive(:update_attributes).
with(valid_attributes.merge(:email => "changed@mail.com",:name=>"changed name"))
put :update, :id => login_user.id, :user => valid_attributes
end
end
and I have something like this for my valid attributes:
def valid_attributes
{
:name => "Test user",
:email=> "user@test.com",
:password => "secret",
:password_confirmation => "secret"
}
end
so what is wrong with my parameters any suggestions?
I am using Rails 3.0.5 with rspec 2.6.0…
The failure message is telling you exactly what’s going on: any instance of
Useris expectingupdate_attributeswith a hash including:email => "changed@mail.com", but it’s getting:email => "user@test.com"because that’s what’s invalid_attributes. Similarly, it’s expecting:name => "changed_name", but gets:name => "Test user"because that’s what’s invalid_attributes.You can simplify this example and avoid this confusion. There is no need to use
valid_attributeshere becauseshould_receiveintercepts theupdate_attributescall anyhow. I usually do this like so:This way the expected and actual values are right in the example and it makes clear that it doesn’t really matter what they are: whatever hash is passed in as
:useris passed directly toupdate_attributes.Make sense?