gpt4 book ai didi

ruby-on-rails - FactoryGirl initialize_with 多个参数

转载 作者:数据小太阳 更新时间:2023-10-29 08:20:12 25 4
gpt4 key购买 nike

我有一个在初始化中有多个参数的模型,其中一个参数在实例化模型的方法中使用:

def initialize(sha, message, repo)
sha = commit.sha
message = commit.message
associate_with(repo)
end

我正在尝试创建一个使用这些参数对其进行初始化的工厂,但是在尝试这样做时出现参数数量错误错误:

FactoryGirl.define do
factory :commit do
intialize_with { new("test_sha", "test_msg", "test_repo") }
end
end

但这给了我错误的参数数量(0 代表 3)。是否无法将多个参数传递给 initialize_with

最佳答案

上面的 initialize 方法是否用于 Commit 类,因为这就是您调用的 Commit.new("test_sha", "test_msg", "test_repo ")

因为我怀疑这是否适用于 Commit

FactoryGirl.define do
factory :commit do
sha "test_sha"
message "test_message"
repo "test_repo"
intialize_with { new(sha,message,repo) }
end
end

这会调用

Commit.new({sha: "test_sha", message: "test_message", repo: "test_repo"})

然后你必须正确地初始化你的其他对象,比如

FactoryGirl.define do
factory :my_other_class do
initialize_with { new('test_sha', 'test_msg', 'test_repo') }
end
end

这将调用 MyOtherClass.new("test_sha", "test_msg", "test_repo") 尽管这看起来有缺陷,因为您期望 MyOtherClass 引用提交并且正在覆盖 shamessage 也许更多的代码会有用

关于ruby-on-rails - FactoryGirl initialize_with 多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043537/

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