【发布时间】:2016-01-16 17:39:25
【问题描述】:
我正在学习使用 RSpec 进行测试。我的测试无法正常工作。
我的模特:
class User < ActiveRecord::Base
has_secure_password
# Validation macros
validates_presence_of :name, :email
validates_uniqueness_of :email, case_sensitive: false
end
我的工厂:
FactoryGirl.define do
factory :user do
name "Joe Doe"
email "joe@example.com"
password_digest "super_secret_password"
end
end
还有我的规格:
require 'rails_helper'
RSpec.describe User, type: :model do
user = FactoryGirl.build(:user)
it 'has a valid factory' do
expect(FactoryGirl.build(:user)).to be_valid
end
it { is_expected.to respond_to(:name) }
it { is_expected.to respond_to(:email) }
it { is_expected.to respond_to(:password) }
it { is_expected.to respond_to(:password_confirmation) }
it { expect(user).to validate_presence_of(:name) }
it { expect(user).to validate_presence_of(:email) }
it { expect(user).to validate_presence_of(:password) }
it { expect(user).to validate_uniqueness_of(:email).case_insensitive }
end
我预计这个测试会通过。但我得到了这个结果:
失败:
1) 用户应该验证 :email 是不区分大小写的唯一 失败/错误:它 { expect(user).to validate_uniqueness_of(:email).case_insensitive }
User did not properly validate that :email is case-insensitively unique. The record you provided could not be created, as it failed with the following validation errors: * name: ["can't be blank"] # ./spec/models/user_spec.rb:18:in `block (2 levels) in <top (required)>'在 0.34066 秒内完成(文件加载需要 1.56 秒)9 示例,1 次失败
失败的例子:
rspec ./spec/models/user_spec.rb:18 # 用户应该验证 :email 是不区分大小写的唯一性
我错过了什么?
更新
我认为这是一个错误:https://github.com/thoughtbot/shoulda-matchers/issues/830
【问题讨论】:
-
您是否在测试之间清除数据库?
-
@CodeGnome,是的......我是
-
@CodeGnome 我尝试禁用清理数据库的脚本,但错误依旧
标签: ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails