Saturday 15 May 2010

ruby on rails - Rspec not showing the updates on a User -


This is my app when testing the end point of an API related to a user:

  Refer to "update a user" reference (: user) {User.create! } "This should allow me to update a user without an email" put "/api/v1/users/#{user.id}", {: user = & gt; {: Firstname = & gt; 'Willie'}}. To_json, {'CONTENT_TYPE' = & gt; 'Application / Jason', 'HTTP_AUTHORIZATION' = & gt; "Tokon token = \" # {auth_token.access_token} \ ""} user.inspect end   

And the controller action I am testing looks like this: < Pre> deb start start user = user.fund (param [: id]) if @user.updatedates (param [: user]) p. User Inspector Jasonson presents: @User ,: Except = gt; [: Made_at, update_at] and render to jason: {: errors = & gt; @ User.errors},: Status = & gt; : Unprocessable_entity end rescue ActiveRecord :: RecordNotFound head: not_found End end

Surprisingly, in the controller @ user.inspect indicates:

  " # & Lt; User ID: 2, first_name: \ "willy \", last_name: zero, email: zero, state: zero, created_t: \ "2013-06-22 11: 21: 22 \", updated : \ "2013-06 -22 11:21:22 \" & gt;   

and user.inspect in rspec, immediately after calling the controller, looks like this:

  "# & lt; User ID: 2, First_name: zero, last_name: zero, email: zero, state: zero, created_at: \ "2013-06-22 11: 21: 22 \", updated: \ "2013-06-22 11:21: 22 \ "& Gt;"   

Why not get updates? I mean, I have tested it manually and the database is updated correctly.

What am I missing here?

In the rspec example, you user method should be let , Which gives an ActiveRecord object, your controller is creating a different object, which points to the same database entry. Change in DB is not reflected in the RSPC example in the user item, because there is no callback mechanism that will notify it to change. Using

#reload should solve your problem on the AR object in the test because it is reloading data from DB.

No comments:

Post a Comment