我有以下测试:
test "should get create" do
sign_in(FactoryGirl.create(:user))
assert_difference('Inquery.count') do
post :create, FactoryGirl.build(:inquery)
end
assert_not_nil assigns(:inquery)
assert_response :redirect
end
我一直在接受:
2) Error:
test_should_get_create(InqueriesControllerTest):
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, License number has already been taken
我不明白为什么我在这个特定的测试中遇到这个错误,当我有一个非常类似的测试:
test "should get create" do
sign_in(FactoryGirl.create(:user, admin: true))
assert_difference('Event.count') do
post :create, FactoryGirl.build(:event)
end
assert_not_nil assigns(:event)
assert_response :success
end
这很好。显而易见的区别是admin:true line,但这并不像我怀疑的那样有效。
添加了:
User_factory.rb
factory :user do
first_name "John"
last_name "Doe"
email "example@example.com"
password "foobar"
password_confirmation "foobar"
license_number '12345'
state 'AZ'
specialty 'Neurosurgery'
end
答案 0 :(得分:1)
您的User
验证失败,因为您的工厂正在为您的测试设置新用户,但您的数据库之间没有清除。将工厂更改为这样,以便每次创建User
时电子邮件和license_number都是唯一的:
factory :user do
first_name "John"
last_name "Doe"
sequence(:email) { |n| "example#{n}@example.com" }
password "foobar"
password_confirmation "foobar"
sequence(:license_number) { |n| "12345#{n}" }
state 'AZ'
specialty 'Neurosurgery'
end