rspec模型测试唯一性

时间:2015-05-12 21:31:53

标签: ruby ruby-on-rails-4 rspec3

validates_uniqueness_of折旧后使用rails 4进行测试的正确语法是什么?

在我的模特中......

# prior to Rails 4
validates_uniqueness_of :number

# Rails 4
validates :number, uniqueness: true

在我的测试文件中......

# prior to rails 4
it {should validate_uniqueness_of(:number)}

此测试失败......

# this fails but with the correct error message expected
it "validates the uniqueness of number" do
  Fabricate(:wo, number: "494949")
  expect{Fabricate.build(:wo, number: "494949")}.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Number has already been taken")
end

我做错了什么?

1 个答案:

答案 0 :(得分:1)

Calling git clone username git@example.com:root/test.git won't actually run the validations. Try build.