【问题标题】:undefined method `symbolize_keys' when using factory girl traits使用工厂女孩特征时未定义的方法“symbolize_keys”
【发布时间】:2015-12-09 19:40:24
【问题描述】:

我正在使用 ruby​​ v1.9.3 和 factory_girl v2.2 并尝试设置特征。但是当我尝试使用我的特质时,我得到了这个错误。是不是因为我用的是ruby版本?

 Failure/Error: let!(:notification) { create(:notification, :confirmable) }
 NoMethodError:
   undefined method `symbolize_keys' for :confirmable:Symbol
 # ./spec/controllers/api/v1/sms_controller_spec.rb:13:in `block (3 levels) in <top (required)>

这是我的工厂:

FactoryGirl.define do
  factory :notification do
    smssid { Faker::Number.number(10) }
    msg    { Faker::Lorem.sentence(3) }
    to     { Faker::PhoneNumber.phone_number }

    trait :confirmable do
      confirmable  "true"
    end
  end
end

规格:

require 'spec_helper'

describe Api::V1::SmsController do

  let!(:user) { create(:user) }

  describe "GET /api/v1/user/sms" do

    let!(:notification) { FactoryGirl.create(:notification, :confirmable) }

    context "a user replies to a confirmation with c" do
      it "does something" do
        get :incomming_message, :sid => notification.smssid,
                                :to => notification.to,
                                :from => user.phone_number,
                                :body => "c",
                                :status => "recieved",
                                :format => :json
        notification.reload
        notification.confirmed.should eq(true)
      end
    end
  end
end

*** 编辑,用我的规范文件更新

【问题讨论】:

  • 你的规范文件是什么样子的
  • 对于旧版本的 FactoryGirl,我认为您使用特征的方式不同,例如,github.com/thoughtbot/factory_girl/blob/v2.2.0/…
  • 我会尽快更新您的应用程序,不再支持 Ruby 1.9.3 并且不安全。正如@DaveNewton 指出的那样,您的问题似乎还在于您使用的是带有较新文档的过时版本的 FactoryGirl。
  • @DaveNewton 我查看了文档,似乎特征设置正确。但是,他们没有给出任何关于在建造工厂时使用特性的例子。
  • 哪些文档?哪个版本的FG? AFAIK 错误消息和 2.2 文档都表明您没有正确使用它来满足 create 在您的版本中的期望。

标签: ruby-on-rails ruby factory-bot


【解决方案1】:

不,您使用的 Ruby 版本与 symbolize_keys 无关,因为它不是 Ruby 核心的一部分。 1.9.3 和 2.2.2 都不包含它。相反,symbolize_keys 是 Hash 类的 ActiveSupport 扩展。您必须加载 Active 支持才能使其正常工作。请参阅:Rails-4.2.5 : Object::HashWithIndifferentAccess < Hash

由于这是一个我错过的 RoR 问题(过失),问题显然在于将 confirmable 声明为符号而不是哈希。但是,无论哪种情况,使用的 Ruby 版本都与问题无关。

【讨论】:

  • ...“问题出在你传入的参数上。”是的,但是 OP 是在问 为什么 这是一个问题,而不是确认这是一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
相关资源
最近更新 更多