gpt4 book ai didi

ruby-on-rails - 如何让工厂可以使用辅助方法? ( rails ,Rspec)

转载 作者:行者123 更新时间:2023-12-05 02:56:48 25 4
gpt4 key购买 nike

我试图在工厂内部调用辅助方法,但它总是返回为未找到方法。这是辅助方法

/spec/helpers/factories.rb

module Helpers
module Factories

def array_of_fakers(faker_class, field, number_of_elements)
faker_array = Array.new
number_of_elements.times do
factory_array.push(class_eval(faker_class).send(field))
end
faker_array
end

end
end

它是这样称呼的...

factory :salesman do
clients { Helpers::Factories.array_of_fakers("Faker::Company", "name", rand(1..5)) }
...
end

我已经尝试在 rails_helper、spec_helper 和文件本身中要求,但都返回相同的结果。我也尝试过不包括模块名称和方法名称,但这也不起作用。这可能吗?

最佳答案

查看 factory_bot GETTING_STARTED.md 中推荐的 spec/support/factory_bot.rb 配置文件设置文档...

就我而言,我没有使用 rails,但这对我有用:

# spec/support/factory_bot.rb

# frozen_string_literal: true

require 'factory_bot'

# Add this...
#
# Require the file(s) with your helper methods. In my case, the
# helper methods were under Support::FileHelpers.
require_relative 'file_helpers'

# This is just the standard setup recommended for inclusion in a non-rails app.
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods

config.before(:suite) do
FactoryBot.find_definitions
end
end

# ...and this...
# Include your helpers.
FactoryBot::SyntaxRunner.send(:include, Support::FileHelpers)

关于ruby-on-rails - 如何让工厂可以使用辅助方法? ( rails ,Rspec),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60116430/

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