Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec case for rails 4 patch :update

I am using Devise in my app, and trying to write a spec for updating email ID. The UI works, but the spec is failing. I am reloading the user object as well, before testing against changed email ID.

One thing that I do observe is that there is UPDATE USERS statement being run on the database.

What is the correct way to write the spec?

RSpec spec:

it "should update email" do
  @user = subject.current_user
  patch :update, id: @user, user: {:email => "[email protected]"}
  @user.reload
  expect(@user.email).to eq("[email protected]")
end

RSpec Error Log:

1) Users::RegistrationsController logged in user should update email
   Failure/Error: expect(@user.email).to eq("[email protected]")

   expected: "[email protected]"
        got: "[email protected]"

   (compared using ==)

test.log:

ActiveRecord::SchemaMigration Load (0.4ms)  SELECT "schema_migrations".* FROM "schema_migrations"
   (0.2ms)  BEGIN
   (0.4ms)  SAVEPOINT active_record_1
  User Exists (0.9ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
  SQL (4.1ms)  INSERT INTO "users" ("created_at", "email", "encrypted_password", "first_name", "last_name", "updated_at")     VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["created_at", Sat, 22 Jun 2013 13:12:28 UTC +00:00], ["email", "[email protected]"], ["encrypted_password", "$2a$04$LIRWzphxstpCLTuZjicA..YMW.Ei2V/LlYWP32gfx39nBjhFg5tLe"], ["first_name", "John"], ["last_name", "Doe"], ["updated_at", Sat, 22 Jun 2013 13:12:28 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 457 ORDER BY "users"."id" ASC LIMIT 1
Processing by Users::RegistrationsController#update as HTML
  Parameters: {"id"=>"457", "user"=>{"email"=>"[email protected]"}}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 457]]
  User Exists (0.4ms)  SELECT 1 AS one FROM "users" WHERE ("users"."email" = '[email protected]' AND "users"."id" !=     457) LIMIT 1
  Rendered devise/registrations/edit.html.slim within layouts/application (0.2ms)
Completed 200 OK in 73ms (Views: 4.0ms | ActiveRecord: 1.0ms)
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 457]]
   (0.2ms)  ROLLBACK
like image 432
Subhash Avatar asked Mar 24 '23 11:03

Subhash


1 Answers

Try

it "should update email" do
  @user = subject.current_user
  patch :update, id: @user, user: {:email => "[email protected]"}
  @user.reload.email.should == "[email protected]"
end

Or alternatively

it "should update email" do
  @user = subject.current_user
  expect {
    patch :update, id: @user, user: {:email => "[email protected]"}
    @user.reload
  }.to change(@user, :email).to("[email protected]")
end
like image 69
spullen Avatar answered Apr 02 '23 04:04

spullen