【问题标题】:Get Errors From FactoryGirl.lint从 FactoryGirl.lint 获取错误
【发布时间】:2014-09-02 20:27:44
【问题描述】:
我继承了一些并没有真正发挥作用的 FactoryGirl 工厂,我正试图将它们淘汰。其中一部分是使用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 上的文档非常稀少
【问题讨论】:
标签:
ruby-on-rails
factory-bot
rspec-rails
【解决方案1】:
循环通过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
来自 FactoryGirl wiki 的This script 展示了如何使用 RSpec 自动检查并使用 Guard 始终验证工厂是否有效。