如何解决factory_girl参数数量错误错误
#rspec test code
@room = FactoryGirl.build(:room)
#factory definition
factory :room do
length {10}
width {20}
end
#code implementation
class Room
attr_accessor :length, :width
def initialize(length,width)
@length = length
@width = width
end
end
尝试构建 @room 时运行 rspec 会导致此错误
<块引用>参数错误: 参数数量错误(0 代表 2)
#rspec test code
@room = FactoryGirl.build(:room)
#factory definition
factory :room do
length {10}
width {20}
end
#code implementation
class Room
attr_accessor :length, :width
def initialize(length,width)
@length = length
@width = width
end
end
Running rspec results in this error when trying to build the @room
ArgumentError:
wrong number of arguments (0 for 2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在确实如此。在版本 4.1 上测试:
结束
参考:文档
Now it does. Tested on version 4.1:
end
Reference: documentation
FactoryGirl
当前不支持带参数的初始值设定项。因此,当您运行build
时,当它尝试执行Room.new
时,它会失败。一个简单的解决方法可能是在测试设置中对类进行猴子修补以解决此问题。这不是理想的解决方案,但您将能够运行测试。
因此,您需要执行以下任一操作(仅在您的测试设置代码中):
或者
此处讨论该问题:
https://github.com/thoughtbot/factory_girl/issues/42
...这里:
https://github.com/thoughtbot/factory_girl/issues/19
FactoryGirl
does not currently support initializers with arguments. So it fails when it's trying to doRoom.new
when you runbuild
.One simple workaround for this might be to monkey-patch your classes in your test setup to get around this issue. It's not the ideal solution, but you'll be able to run your tests.
So you'd need to do either one of these (just in your test setup code):
or
The issue is discussed here:
https://github.com/thoughtbot/factory_girl/issues/42
...and here:
https://github.com/thoughtbot/factory_girl/issues/19
对我有用的是启用 FactoryBot linting 的调试输出:
请参阅 文档了解详细信息
What was useful for me, was enabling debug output for FactoryBot linting:
see the documentation for the details