gpt4 book ai didi

ruby-on-rails - 从 FactoryGirl.lint 获取错误

转载 作者:行者123 更新时间:2023-12-04 19:43:08 25 4
gpt4 key购买 nike

我继承了一些实际上不起作用的 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 上的文档极其稀少

最佳答案

循环 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 wiki 的内容展示了如何使用 RSpec 自动检查并使用 Guard 始终验证工厂是否有效。

关于ruby-on-rails - 从 FactoryGirl.lint 获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com