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

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

概述我在我的应用程序中使用Devise,并尝试编写更新电子邮件ID的规范.用户界面工作,但规格失败.在测试更改的电子邮件ID之前,我也在重新加载用户对象. 我观察到的一件事是在数据库上运行UPDATE USERS语句. 编写规范的正确方法是什么? RSpec规格: it "should update email" do @user = subject.current_user patch :u 我在我的应用程序中使用Devise,并尝试编写更新电子邮件ID的规范.用户界面工作,但规格失败.在测试更改的电子邮件ID之前,我也在重新加载用户对象.

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

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

RSpec规格:

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错误日志:

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 (,,,,,) RETURNING "ID"  [["created_at",Sat,22 Jun 2013 13:12:28 UTC +00:00],["email","[email protected]"],["encrypted_password","a$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 1Processing by Users::RegistrationsController#update as HTML  Parameters: {"ID"=>"457","user"=>{"email"=>"[email protected]"}}  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."ID" =  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" =  liMIT 1  [["ID",457]]   (0.2ms)  RolLBACK
解决方法 尝试

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

或者

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

以上是内存溢出为你收集整理的ruby-on-rails – 用于rails 4补丁的Rspec案例:更新全部内容,希望文章能够帮你解决ruby-on-rails – 用于rails 4补丁的Rspec案例:更新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1283801.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存