加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 用于rails 4补丁的Rspec案例:更新

发布时间:2020-12-17 02:45:36 所属栏目:百科 来源:网络整理
导读:我在我的应用程序中使用Devise,并尝试编写更新电子邮件ID的规范.用户界面工作,但规格失败.在测试更改的电子邮件ID之前,我也在重新加载用户对象. 我观察到的一件事是在数据库上运行UPDATE USERS语句. 编写规范的正确方法是什么? RSpec规格: it "should upda
我在我的应用程序中使用Devise,并尝试编写更新电子邮件ID的规范.用户界面工作,但规格失败.在测试更改的电子邮件ID之前,我也在重新加载用户对象.

我观察到的一件事是在数据库上运行UPDATE USERS语句.

编写规范的正确方法是什么?

RSpec规格:

it "should update email" do
  @user = subject.current_user
  patch :update,id: @user,user: {:email => "john.doe@example1.com"}
  @user.reload
  expect(@user.email).to eq("john.doe@example1.com")
end

RSpec错误日志:

1) Users::RegistrationsController logged in user should update email
   Failure/Error: expect(@user.email).to eq("john.doe@example1.com")

   expected: "john.doe@example1.com"
        got: "john.doe@example.com"

   (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" = 'john.doe@example.com' 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","john.doe@example.com"],["encrypted_password","$2a$04$LIRWzphxstpCLTuZjicA..YMW.Ei2V/LlYWP32gfx39nBjhFg5tLe"],["first_name","John"],["last_name","Doe"],["updated_at",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"=>"john.doe@example1.com"}}
  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" = 'john.doe@example1.com' 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

解决方法

尝试

it "should update email" do
  @user = subject.current_user
  patch :update,user: {:email => "john.doe@example1.com"}
  @user.reload.email.should == "john.doe@example1.com"
end

或者

it "should update email" do
  @user = subject.current_user
  expect {
    patch :update,user: {:email => "john.doe@example1.com"}
    @user.reload
  }.to change(@user,:email).to("john.doe@example1.com")
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读