未定义的方法`to_sym'为1:Fixnum

时间:2013-12-24 09:32:01

标签: ruby-on-rails ruby-on-rails-3 rspec factories

我有一个简单的模型,序列作为整数属性。当我将序列设置为1时,它失败了简单的测试用例,即:

  context 'MathFactAttemptData' do
    it 'should insert rows' do
      math_fact_attempt_data  = FactoryGirl.build :math_fact_attempt_data

      @params[:request_params]         = {
          user_data: {
              math_fact_attempt_data: [JSON.parse(math_fact_attempt_data.to_json)]
          }
      }

      initial_math_fact_attempt_data_count = MathFactAttemptData.unscoped.count

      post api_v3_user_data_path, @params
      response.should be_success

      response_body = JSON.parse response.body
      response_body['user_data'].should be_nil
      response_body['seed_data'].should be_nil

      MathFactAttemptData.unscoped.count.should == initial_math_fact_attempt_data_count + 1

    end
  end

工厂:

factory :math_fact_attempt_data do
 association :user, factory: :student
 association :math_fact_attempt, factory: :math_fact_attempt
 association :problem_type, factory: :problem_type
 num1 1
 num2 1
 correct_answer 1
 response 1
 correct 1
 #sequence 1
 time_spent 1
 choice_type "MyString"
 selected_operator "MyString"
end

取消注释序列使测试用例失败,即:

   API v3 POST /user_data.json Entities MathFactAttemptData should insert rows
         Failure/Error: math_fact_attempt_data  = FactoryGirl.build :math_fact_attempt_data
         NoMethodError:
           undefined method `to_sym' for 1:Fixnum
         # ./spec/requests/api/v3/post_user_data_spec.rb:1116:in `block (5 levels) in <top (required)>'

    Finished in 3.7 seconds
    1 example, 1 failure

    Failed examples:

    rspec ./spec/requests/api/v3/post_user_data_spec.rb:1115 # API v3 POST /user_data.json Entities MathFactAttemptData should insert rows

2 个答案:

答案 0 :(得分:8)

正如Peter指出的,sequence是FactoryGirl方法。

尝试此操作来设置序列属性:

FactoryGirl.define do
  factory :math_fact_attempt_data do
    add_attribute :sequence, 1
  end
end

答案 1 :(得分:4)

sequence是一个FactoryGirl方法,它需要一个符号作为它的第一个参数。这就是你得到错误的原因。