这不是我以前读过的某种同步问题。代码非常简单。该模型:
class User < ActiveRecord::Base
attr_accessor :name, :email
validates_uniqueness_of :email, :on => :create, :message => "must be unique"
end
rspec 测试:
require 'spec_helper'
describe User do
before(:each) do
@valid_attributes = {
:name => "Foo Bar",
:email => "foo@bar.com"
}
end
it "should reject duplcate email address" do
User.create!(@valid_attributes)
duplicate_user = User.new(@valid_attributes)
duplicate_user.should_not be_valid
end
end
我运行测试,并收到错误消息:
----------------------------
1)
'User should reject duplcate email address' FAILED
expected #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil> not to be valid
/Users/mac/workspace/rails_space/uniq/spec/models/user_spec.rb:14:
Finished in 0.067908 seconds
1 example, 1 failure
-----------------------------
我运行脚本/控制台,并创建两个具有相同电子邮件地址的用户对象。一切正常,没有出现验证消息,这两个对象都已插入表中。我不知道它有什么问题。
我的 rails 版本是 2.3.8,rspc 是 1.3.0。