Rails 5.2 Rspec 3.7 Pundit 1.10
以下针对我的政策的Rspec测试失败并显示错误:
spec / policies / proofreader / dashboard
describe Proofreader::DashboardPolicy do
subject { described_class }
context 'accessing the proofreader dashboard' do
permissions :index? do
it "denies access to admins" do
expect(subject).not_to permit(FactoryBot.create(:admin))
end
end
end
end
Error: ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
此测试在数据库清理时第一次通过,但随后失败。
我的用户工厂使用序列,因此电子邮件不同:
FactoryBot.define do
factory :user, aliases: [:client] do
sequence(:email) { |n| "tester#{n}@example.com" }
password 123456
password_confirmation 123456
factory :admin do
after(:build) {|user| user.admin = true }
end
factory :proofreader do
after(:build) {|user| user.proofreader = true }
end
factory :admin_proofreader do
after(:build) {|user| user.admin = true && user.proofreader = true}
end
end
end
为什么我会收到此错误以及如何解决?