如何使用FactoryBot在rspec测试中修复'undefined method'枚举?

问题描述 投票:1回答:1

我正在尝试测试为我的应用程序创建的模型,它与其他两个模型相关联,也使用Factory Bot来构建测试,但是它无法识别它,错误返回为:Failure/Error: status :pending //// NoMethodError: undefined method 'status' in 'pending' factory.] >

我正在使用Ruby 2.6.1,Rails 5.2.3,FactoryBot 5.0.2,Rspec 3.8运行该应用程序。我尝试了定义枚举的不同方法。我不知道该怎么办。

型号:

class CollegeWhitelist < ApplicationRecord
    enum status: {pending: 0, approved: 1, rejected: 2}

    has_many :users
    has_many :colleges
end

工厂:

FactoryBot.define do
  factory :college_whitelist do
    association :user
    association :college

    trait :pending do
      status :pending
    end

    trait :approved do
      status :approved
    end

    trait :rejected do
      status :rejected
    end
  end
end

Rspec:

require 'rails_helper'

RSpec.describe CollegeWhitelist, type: :model do
  describe "#consistency " do
    it 'cannot insert the same user for the same college in permissions' do
      @permission = build(:college_whitelist)
      p @permission
    end
  end
end

我期望它首先通过打印对象即可通过测试。

我正在尝试测试为我的应用程序创建的模型,它与其他两个模型相关联,也使用Factory Bot来构建测试,但是它无法识别,错误返回是:...] >

ruby-on-rails ruby rspec ruby-on-rails-5 factory-bot
1个回答
1
投票

这是命名冲突的问题。

您必须将status列的值用大括号括起来,否则它将自称:

© www.soinside.com 2019 - 2024. All rights reserved.