未定义的方法“stringify_keys”使用工厂女孩时
我的 User_spec.rb 中有以下代码块:
@user = { username:'newuser',
email:'[email protected]',
fname:'new',
lname:'user',
password:'userpw',
password_confirmation:'userpw'}
用于使用这些属性创建一个 using 。然而,当我将所有这些属性移至 Factories.rb:
require 'factory_girl'
Factory.define :user do |u|
u.username 'newuser'
u.email '[email protected]'
u.fname 'new'
u.lname 'user'
u.password 'newuserpw'
u.password_confirmation 'newuserpw'
end
并将 user_spec.rb 中的行替换为:
@user = Factory(:user)
与用户模型相关的所有测试都失败了(例如电子邮件、密码、用户名等测试)时,所有这些都给了我
“未定义方法‘stringify_keys’…”
新用户对象
I have the following block of code in my User_spec.rb:
@user = { username:'newuser',
email:'[email protected]',
fname:'new',
lname:'user',
password:'userpw',
password_confirmation:'userpw'}
for creating a using using these attributes. However while I moved all these attributes to Factories.rb:
require 'factory_girl'
Factory.define :user do |u|
u.username 'newuser'
u.email '[email protected]'
u.fname 'new'
u.lname 'user'
u.password 'newuserpw'
u.password_confirmation 'newuserpw'
end
and replace the line in user_spec.rb with:
@user = Factory(:user)
all my tests that related to the User model failed(such as tests for email, password, username etc), all were giving me
"undefined method `stringify_keys' for…"
the new user object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了类似的问题,这是因为我将 FactoryGirl 对象传递给 ActiveRecord create/new 方法(哎呀!)。看起来你在这里也在做同样的事情。
您列出的第一个/顶部@user是值的散列,但第二个/底部@user是您的User对象的实例(由FactoryGirl动态构建)。
如果您在规范中调用类似的内容:
@user 的第一个(散列)版本可以工作,但第二个(对象化)版本不会工作(并抛出“stringify_keys”错误) 。要正确使用 @user 的第二个版本,您应该在规范中包含以下内容:
希望有所帮助。
I had a similar problem, and it was because I was passing a FactoryGirl object to the ActiveRecord create/new method (whoops!). It looks like you are doing the same thing here.
The first/top @user you have listed is a hash of values, but the second/bottom @user is an instance of your User ojbect (built by FactoryGirl on the fly).
If you are calling something like this in your specs:
The first (hashed) version of @user will work, but the second (objectified) version will not work (and throw you the 'stringify_keys' error). To use the second version of @user properly, you should have this in your specs:
Hope that helps.
我们需要查看一个失败测试的示例来进行诊断,但可能会导致这种情况的原因是:在需要属性时发送对象。 内容修复了一个失败的测试:
我曾经通过更改以下
We need to see an example of a failing test to diagnose, but here is one thing that can cause it – sending an object when attributes are required. I once fixed one of my failing tests by changing:
to
@rowanu 回答了你的问题,但让我也布局我的示例以供将来参考:
我的案例失败的是:
请注意,我正在尝试使用预订实例而不是属性哈希来构建
示例:
在spec/factories/domain_factory.rb中我有
@rowanu Answered your question, but let me layout my example too for future reference:
What was failing in my case was:
Note that I am trying to build with a booking instance and not hash of attributes
The working example:
And in spec/factories/domain_factory.rb I have