作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个在初始化中有多个参数的模型,其中一个参数在实例化模型的方法中使用:
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
引用提交并且正在覆盖 sha
和 message
也许更多的代码会有用
关于ruby-on-rails - FactoryGirl initialize_with 多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043537/
我有一个在初始化中有多个参数的模型,其中一个参数在实例化模型的方法中使用: def initialize(sha, message, repo) sha = commit.sha me
我是一名优秀的程序员,十分优秀!