首页 > 解决方案 > 在 rspec 测试期间,超出范围错误,Type::Integer

问题描述

得到一个失败的 rspec 测试,说活动记录超出范围。不知道为什么它失败了

这是错误

Failure/Error: Order.create! params.merge(user: user, subscription: subscription, product: product)

 ActiveModel::RangeError:
   7554736346861994060 is out of range for ActiveModel::Type::Integer with limit 4 bytes

rspec 测试

context 'subscription order params' do
      let(:subscription_order_params) { FactoryBot.attributes_for(:order,
                                                                  party_user_id: subscription_user.party_id,
                                                                  party_subscription_id: subscription.party_id,
                                                                  party_product_id: product.party_id)}

      it 'creates an order that belongs to a subscription customer' do
        post :create, params: { order: subscription_order_params }, as: :json

        expect(response.status).to eq 204
      end
    end

标签: ruby-on-railsrubyrspec

解决方案


在一个 4 字节的INT字段中,您最多可以存储一个2147483647整数。

7554736346861994060 比这个大得多2147483647,它需要超过 4 个字节。

您需要一种不同的数据类型来支持这样的数字。

添加迁移以更改BigInteger应该存储大量数字的列。


推荐阅读