从FactoryGirl.lint获取错误
问题描述:
我继承了一些FactoryGirl工厂,这些工厂并没有真正的工作,我正试图让他们努力。部分原因是使用了FactoryGirl.lint
。然而到目前为止,我已经能够找到它的工厂失败,并且对于任何一个个体,运行从FactoryGirl.lint获取错误
x = FactoryGirl.build :invalid_factory
x.valid? # returns false as expected
x.errors # prints out the validation errors for that object
我希望做的是避免这样做,每个工厂。有没有办法快速得到FactoryGirl.lint
写出每个无效工厂的错误?要通过的标志,要设置的参数?该文件是非常稀疏的.lint
答
循环通过FactoryGirl.factories
执行您的每个工厂检查。
FactoryGirl.factories.map(&:name).each do |factory_name|
describe "#{factory_name} factory" do
# Test each factory
it "is valid" do
factory = FactoryGirl.build(factory_name)
if factory.respond_to?(:valid?)
# the lamba syntax only works with rspec 2.14 or newer; for earlier versions, you have to call #valid? before calling the matcher, otherwise the errors will be empty
expect(factory).to be_valid, lambda { factory.errors.full_messages.join("\n") }
end
end
This script从FactoryGirl维基展示了如何自动使用RSpec的检查,并使用保护始终核实工厂是有效的。