rails 工厂 rspec 测试失败,因为模型中 "only_integer" 验证

rails factory rspec tests failing because of "only_integer" validation in model

我的模型 'price' 有一个工厂,但是当我将价格验证设为 only_integer 时,所有 rspec 测试开始失败。当我尝试使用 rspec 验证工厂时得到的错误是 "Price must be an integer"

这是我的模型文件:

class Price < ApplicationRecord
   belongs_to :expertise
   validates :price,
      :presence => true,
      :numericality => { only_integer: true }
end

这是我的工厂价格:

    FactoryGirl.define do
      factory :price do
        association :expertise, factory: :expertise, strategy: :create
        price 10
        # price Faker::Number.between(1,1000).to_i
      end
    end

这是我的迁移文件:

    class CreatePrices < ActiveRecord::Migration[5.0]
       def change
         create_table :prices do |t|
         t.integer :expertise_id
         t.integer :price
         t.timestamps
       end
     add_index :prices, [:expertise_id], unique: true
     end
   end

编辑:添加 rspec 测试和错误:

控制器rspec测试:

require 'rails_helper'

RSpec.describe PricesController, type: :controller do

  let(:price) {FactoryGirl.create(:price)}

  describe "GET show" do
    it "renders :show template" do
      get :show, params: { id: price.id }
      expect(response).to render_template(:show)
    end
  end
end

我得到的错误:

 1) PricesController GET show renders :show template
    Failure/Error: let(:price) {FactoryGirl.create(:price)}

 ActiveRecord::RecordInvalid:
   Validation failed: Price must be an integer
 # ./spec/controllers/prices_sample_controller.rb:7:in `block (2 levels) in <top (required)>'
 # ./spec/controllers/prices_sample_controller.rb:11:in `block (3 levels) in <top (required)>'

我是 rails 的新手,所以如果我没有提供足够的信息或做了一些非常愚蠢的事情,请原谅。

我不确定这是为什么或如何工作,但我将我的出厂价格值转换为字符串并且所有测试都通过了。

我真的很想知道为什么会发生这种行为。

FactoryGirl.define do
  factory :price do
    association :expertise, factory: :expertise, strategy: :create
    # price Faker::Number.between(1,1000).to_i
    price Faker::Number.between(1,1000).to_s
  end
end