0

我按照Michael Hartl 的 The Ruby on Rails Tutorial Book 进行了修改,添加了用于用户身份验证的设计,并遇到了电子邮件唯一性测试的问题。

# spec/models/user_spec.rb

require 'spec_helper'

describe User do
  before do
    @user = User.new(username: 'ExampleUser',
                     email:    'user@example.com',
                     password: 'passworD123')
    @user.save
  end

  subject { @user }

  it { should respond_to(:email) }
  it { should respond_to(:name) }
  it { should respond_to(:username) }
  it { should respond_to(:password) }

  it { should be_valid }

  describe 'username is already taken' do
    before do
      user_with_same_username = @user.dup
      user_with_same_username.username = @user.username.upcase
      user_with_same_username.email = 'a@b.c'
      user_with_same_username.save
    end

    it { should_not be_valid }
  end

  describe 'email address is already taken' do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.username = 'differentUsername'
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

  .
  .

失败:

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

电子邮件的唯一性已设置,尽管不需要,因为 Devise 已经这样做了。我更改user_with_same_email.saveuser_with_same_email.save!,然后我收到了电子邮件地址的验证错误,因为它已经被占用:

1) User email address is already taken
   Failure/Error: user_with_same_email.save!
   ActiveRecord::RecordInvalid:
     Validation failed: Email has already been taken
   # ./spec/models/user_spec.rb:101:in `block (3 levels) in <top (required)>'

的返回值为user_with_same_email.savefalse 并it { should_not be_valid }检查这种情况,但为什么测试仍然失败?

4

1 回答 1

2

关于您的实际问题,原因很简单:您没有测试正确的subject

其他指南:

  • 如果您知道对象无效,请不要保存它,您只是在浪费时间。

  • 与您的第一个规格相同,您真的需要一个持久对象吗?我猜不会..

  • 最后但同样重要的是:考虑使用工厂,它是可重用的,并且可以让您进行更一致的测试。

这是您的规格可能的样子:

require 'spec_helper'

describe User do
  let(:user_attributes) { {
      username: 'ExampleUser',
      email:    'user@example.com',
      password: 'passworD123'
    }
  }

  subject(:user) { User.new(user_attributes) }

  it { should respond_to(:email) }
  it { should respond_to(:name) }
  it { should respond_to(:username) }
  it { should respond_to(:password) }
  it { should be_valid }

  context 'with existing user in db' do
    before(:each) { user.save }

    describe 'username is already taken' do
      subject(:user_with_same_username) { User.new(user_attributes.merge(email: 'another@email.com')) }
      it { should_not be_valid }
    end

    describe 'email address is already taken' do
       subject(:user_with_same_email) { User.new(user_attributes.merge(username: 'another name')) }
       it { should_not be_valid }
    end
  end
end
于 2013-02-17T10:10:44.227 回答