加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:07:22 所属栏目:百科 来源:网络整理
导读:我继承了许多没有真正起作用的FactoryGirl工厂,我正试图将它们带到鼻烟.部分原因是使用了FactoryGirl.lint.然而,到目前为止,我已经能够找到哪些工厂失败了,而且对于任何一个工厂都运行了 x = FactoryGirl.build :invalid_factoryx.valid? # returns false as
我继承了许多没有真正起作用的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

FactoryGirl wiki中的This script显示了如何使用RSpec自动检查并使用Guard始终验证工厂是否有效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读