首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 4修补程序的Rspec案例:更新

rails 4修补程序的Rspec案例:更新
EN

Stack Overflow用户
提问于 2013-06-22 20:47:00
回答 1查看 3.7K关注 0票数 4

我正在我的应用程序中使用Devise,并试图编写用于更新电子邮件ID的规范。UI可以工作,但规范失败。我也正在重新加载用户对象,然后测试更改的电子邮件ID。

我确实观察到的一件事是,数据库上正在运行UPDATE USERS语句。

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

RSpec规范:

代码语言:javascript
复制
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错误日志:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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", 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"=>"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
EN

回答 1

Stack Overflow用户

发布于 2013-06-22 20:53:33

试一试

代码语言:javascript
复制
it "should update email" do
  @user = subject.current_user
  patch :update, id: @user, user: {:email => "john.doe@example1.com"}
  @user.reload.email.should == "john.doe@example1.com"
end

或者另选地

代码语言:javascript
复制
it "should update email" do
  @user = subject.current_user
  expect {
    patch :update, id: @user, user: {:email => "john.doe@example1.com"}
    @user.reload
  }.to change(@user, :email).to("john.doe@example1.com")
end
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17250877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档