【问题标题】:Rspec test help - email uniqueness test failing yet it works in my appRspec 测试帮助 - 电子邮件唯一性测试失败但它在我的应用程序中有效
【发布时间】:2014-07-24 17:17:28
【问题描述】:

我一直在创建一个基于 Hartl 课程的应用程序,并添加了 Organization 的概念,其中 has_many 用户。这些测试是 Hartl 推荐的所有标准测试,直到指南的第 9.2 节。由于将组织实施到应用程序中,其中一个测试用例在“当电子邮件地址已被占用时”失败 - 这应该会阻止用户使用相同的电子邮件地址注册两次。奇怪的是,这在应用程序本身中有效(表单错误 - “用户电子邮件地址已被使用”抛出)但在我的测试中没有。请您帮忙说明一下为什么会出现这种情况?

用户代码:

class User < ActiveRecord::Base
belongs_to :organization
#accepts_nested_attributes_for :organization
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
validates :organization, presence: true

组织机构代码:

class Organization < ActiveRecord::Base
validates :organization_name, presence: true, length: { maximum: 50 }, uniqueness: true 
has_many :users, :inverse_of => :organization
accepts_nested_attributes_for :users

用户规格:

require 'spec_helper'

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

 subject { @user }

 it { should respond_to(:name) }
 it { should respond_to(:email) }
 it { should respond_to(:password_digest) }
 it { should respond_to(:password) }
 it { should respond_to(:password_confirmation) }
 it { should respond_to(:remember_token) }
 it { should respond_to(:authenticate) }
 it { should be_valid }
...
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

工厂女孩代码:

FactoryGirl.define do
factory :organization do
    organization_name       "Example Org"

    trait :wrong do
        organization_name   "Wrong Org"
    end

    trait :also_wrong do
        organization_name   "Another Wrong Org"
    end
end

factory :user do
    association :organization
    name                    "Example Name"
    email                   "email@example.com"
    password                "foobar"
    password_confirmation   "foobar"

    trait :wrong_org do
        association :organization, :factory => [:organization, :wrong]
    end

    trait :wrong_org2 do
        association :organization, :factory => [:organization, :also_wrong]
    end
end
end

Rails 控制台抛出的错误如下:

1) 电子邮件地址已被占用的用户不应该是有效的 失败/错误:它 { should_not be_valid } 预期 #&lt;User id: 5287, name: "Example Name", email: "email@example.com", created_at: "2014-07-22 15:04:33", updated_at: "2014-07-22 15:04:33", password_digest: "$2a$04$jrxyuz9e574BoaAhZm6xkOUeAY5spyDut2CCEvAykMu...", organization_id: 5025, remember_token: "339dfafcac7bc5925dbf4e44f60a782f3bbbaa1b"&gt;.valid? 返回 false,得到 true

我尝试更改测试中的代码,但无论我做什么,它都会不断抛出错误。如上所述,当我在本地服务器中打开应用程序时,我可以使用所有功能,而当我尝试使用重复的电子邮件地址注册时,它不会让我这样做。我的测试代码有什么问题?

【问题讨论】:

  • @user 仍然有效,并且永远有效。 user_with_same_email 无效。

标签: ruby-on-rails ruby rspec


【解决方案1】:

@user 完全有效:

  • 您创建主题,@user。这是有效的
  • 您创建user_with_same_email
  • 那个 用户无效,因为它与@user 拥有相同的电子邮件
  • 保存 user_with_same_email 会返回 false,但您的测试中并未检查这一点
  • 重复用户未保存在数据库中
  • 原用户仍然有效

正确的测试只是#dup用户(或使用相同的电子邮件创建一个新的),然后检查 new 记录是否无效。

【讨论】:

  • 谢谢,但我已经尝试建立一个新用户(甚至创建一个使用相同电子邮件创建新用户的新工厂),但仍然会抛出错误。此外,自添加组织功能以来,此测试没有更改,并且提前通过了。也许我做错了什么。
  • 其实我最后还是用你的回答解决了这个问题。谢谢你让我走上正轨。我用相同的电子邮件地址创建了一个新用户(不使用 FactoryGirl,因为 ActiveRecord 不喜欢这种方法),它解决了这个问题。感谢您的帮助。
  • 好吧,如果你用 FactoryGirl 创建一个新记录,它会在失败时引发异常。你可以用 FG build 它,然后在上面运行你的测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多