未定义的方法“stringify_keys”使用工厂女孩时

发布于 2024-12-13 04:12:14 字数 967 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年里的时光 2024-12-20 04:12:14

我遇到了类似的问题,这是因为我将 FactoryGirl 对象传递给 ActiveRecord create/new 方法(哎呀!)。看起来你在这里也在做同样的事情。

您列出的第一个/顶部@user是值的散列,但第二个/底部@user是您的User对象的实例(由FactoryGirl动态构建)。

如果您在规范中调用类似的内容:

user = User.new(@user)

@user 的第一个(散列)版本可以工作,但第二个(对象化)版本不会工作(并抛出“stringify_keys”错误) 。要正确使用 @user 的第二个版本,您应该在规范中包含以下内容:

user = Factory(: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:

user = User.new(@user)

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:

user = Factory(:user)

Hope that helps.

戒ㄋ 2024-12-20 04:12:14

我们需要查看一个失败测试的示例来进行诊断,但可能会导致这种情况的原因是:在需要属性时发送对象。 内容修复了一个失败的测试:

post :create, organization: @organization

我曾经通过更改以下

post :create, organization: @organization.attributes

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:

post :create, organization: @organization

to

post :create, organization: @organization.attributes
始终不够 2024-12-20 04:12:14

@rowanu 回答了你的问题,但让我也布局我的示例以供将来参考:

我的案例失败的是:

@user = User.new user_attr
@user.bookings_build(Booking.new booking_attr)

请注意,我正在尝试使用预订实例而不是属性哈希来构建

示例:

user_attr_hash    = FactoryGirl.attributes_for(:user)
booking_attr_hash = FactoryGirl.attributes_for(:booking)

@user = User.new user_attr_hash
@user.bookings.build(booking_attr_hash)

在spec/factories/domain_factory.rb中我有

FactoryGirl.define do
  factory :user do
    # DEFAULT USER...
    password "123123123"
    email "[email protected]"
    # there rest of attributes set...
  end

  factory :booking do
    start_date Date.today
    end_date Date.today+3
    # the rest of attributes
  end
end

@rowanu Answered your question, but let me layout my example too for future reference:

What was failing in my case was:

@user = User.new user_attr
@user.bookings_build(Booking.new booking_attr)

Note that I am trying to build with a booking instance and not hash of attributes

The working example:

user_attr_hash    = FactoryGirl.attributes_for(:user)
booking_attr_hash = FactoryGirl.attributes_for(:booking)

@user = User.new user_attr_hash
@user.bookings.build(booking_attr_hash)

And in spec/factories/domain_factory.rb I have

FactoryGirl.define do
  factory :user do
    # DEFAULT USER...
    password "123123123"
    email "[email protected]"
    # there rest of attributes set...
  end

  factory :booking do
    start_date Date.today
    end_date Date.today+3
    # the rest of attributes
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文