从FactoryGirl.lint获取错误

ABM*_*gil 4 ruby-on-rails rspec-rails factory-bot

我继承了许多没有真正工作的FactoryGirl工厂,我试图把它们搞得很好.部分原因是使用FactoryGirl.lint.然而,到目前为止,我已经能够找到哪些工厂失败了,对于任何一个工厂来说,它都能运行

x = FactoryGirl.build :invalid_factory
x.valid? # returns false as expected
x.errors # prints out the validation errors for that object
Run Code Online (Sandbox Code Playgroud)

我想做的是避免为每个工厂做这件事.有没有办法快速FactoryGirl.lint写出每个无效工厂的错误?要传递的标志,要设置的参数?文档非常稀少.lint

Jay*_*ell 6

循环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
Run Code Online (Sandbox Code Playgroud)

FactoryGirl wiki中的这个脚本显示了如何使用RSpec自动执行检查,并使用Guard始终验证工厂是否有效.