从初始化属性切换到Factory时,rspec测试失败 [英] rspec test failure when switching from initializing attributes to Factory

查看:81
本文介绍了从初始化属性切换到Factory时,rspec测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说毫无意义,希望有人可以提供帮助.使用工厂创建用户时,我遇到一个测试失败(看似相似的测试通过).

This makes no sense to me, hope someone can help. I'm getting a single test failure when creating a user using a factory (seemingly similar tests pass).

在我的user_spec.rb测试文件中,该用户最初是这样创建的.使用这种初始化方法,所有测试都会通过.

In my user_spec.rb test file, the user was originally created like this. Using this initialization approach, all tests pass.

用户创建

user_spec.rb (无工厂)

describe User do

  before do
  @user = User.new(name: "Example User", email: "user@example.com", 
                   password: "foobar", password_confirmation: "foobar", 
                   username: "username")
  end

该应用程序的其他大多数测试都使用工厂.因此,决定将上述内容替换为致电工厂.

Most of the other tests for the app use factories. So, decided to replace the above with call to factory.

user_spec.rb (带有工厂)

describe User do

  before do
  @user = FactoryGirl.create(:user)
  end


工厂


FACTORY

工厂看起来像这样.

factories.rb

FactoryGirl.define do
      factory :user do
        sequence(:name)  { |n| "Person #{n}" }
        sequence(:email) { |n| "person_#{n}@example.com" }
        password              "foobar"
        password_confirmation "foobar"
        sequence(:username) { |n| "username_#{n}" }


测试失败


TEST FAILURE

这是使用工厂创建用户时的测试失败.

Here is the test failure when using the factory to create the user.

user_spec.rb

Failures:

  1) User when email address is already taken 
     Failure/Error: it { should_not be_valid }
       expected valid? to return false, got true
     # ./spec/models/user_spec.rb:101:in `block (3 levels) in <top (required)>'

Finished in 12.61 seconds
178 examples, 1 failure, 2 pending


测试


TEST

这是测试的一部分,当切换到出厂时会失败.

Here is the part of the test that fails when switching to the factory.

describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
        user_with_same_email.email = @user.email.upcase
        user_with_same_email.save
    end

    it { should_not be_valid }
  end


检查用户和USER.DUP


INSPECTION OF USER AND USER.DUP

我的第一个想法可能是在工厂创建的用户上使用.dup引起了问题.这是@user和@ user.dup在上述测试中使用时的输出(将puts @user.inspectputs user_with_same_email.inspect行添加到describe块中.这两个puts都在user_with_same_email.save之后)

My first thought was perhaps using the .dup on the factory created user was causing problems. Here is the @user and the @user.dup when output from their use in the test above (by adding the lines puts @user.inspect and puts user_with_same_email.inspect into the describe block. Both of these puts are after the user_with_same_email.save).

完全没有(测试通过)

@用户

#<User id: nil, name: "Example User", email: "user@example.com", 
created_at: nil, updated_at: nil, 
password_digest: "$2a$04$I/71i.fpTwfp4PqRwAvU4eUEjEkW/wubx6uVBqfNBShq...", 
remember_token: nil, admin: false, username: "username">

user_with_same_email

#<User id: 1178, name: "Example User", email: "USER@EXAMPLE.COM", 
created_at: "2012-04-24 06:01:07", updated_at: "2012-04-24 06:01:07", 
password_digest: "$2a$04$I/71i.fpTwfp4PqRwAvU4eUEjEkW/wubx6uVBqfNBShq...", 
remember_token: "rMS9jM0d4lobLc-A-pTTqA", admin: false, username: "username">

工厂(测试失败)

@用户

#<User id: 739, name: "Person 67", email: "person_67@example.com", 
created_at: "2012-04-24 05:54:28", updated_at: "2012-04-24 05:54:28", 
password_digest: "$2a$04$n4tToBVM2elz92cfHPKvte6dfHSBj4jDxG.w6DyKtGUR...", 
remember_token: "fy2iifmhXVTFa1__d1dBJg", admin: false, username: "username_67">

user_with_same_email

#<User name: "Person 67", email: "PERSON_67@EXAMPLE.COM", 
created_at: nil, updated_at: nil, 
password_digest: "$2a$04$n4tToBVM2elz92cfHPKvte6dfHSBj4jDxG.w6DyKtGUR...", 
remember_token: "fy2iifmhXVTFa1__d1dBJg", admin: false, username: "username_67">

上面突出的是,@ user在不使用工厂的情况下尚未被保存,而在使用工厂时@user被保存.在这种情况下,我只是看不到它如何影响测试.

What stands out above is that @user is not already saved without using the factory, whereas when using the factory @user is saved. I'm just not seeing how this effects the test in this case.

任何帮助将不胜感激.谢谢!

Any help would be appreciated. Thanks!

(以上代码来自Michael Hartl的Rails教程,我正在研究某些应用功能扩展,他在本教程的结尾处提到了.)

(The above code comes from Michael Hartl's Rails Tutorial, I'm working on some of the app feature extensions he mentions at the end of the tutorial.)

推荐答案

工厂将记录放入数据库中.除非保存,否则User.new不会.原始测试不会检查是否创建了user_with_same_email,而是因为电子邮件存在而无法创建"it"(主题{@user}).对@user进行更改后,我不得不重写测试:

The factory puts the record in the DB. User.new doesn't until it is saved. The original test doesn't check if user_with_same_email gets created, rather that 'it' (subject { @user }) can't be created because the email exists. After making your change to the @user, I had to rewrite the test:

describe "when email address is already taken" do
  let (:user_with_same_email)  { @user.dup }

  specify { user_with_same_email.should_not be_valid }
end

这篇关于从初始化属性切换到Factory时,rspec测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆