Reputation: 63
I'm trying to test an model I had created for my application, it has association with two other models, also using Factory Bot to build the test, however it does not recognize it, the error return is: Failure/Error: status :pending //// NoMethodError: undefined method 'status' in 'pending' factory.
I'm running the application with Ruby 2.6.1, Rails 5.2.3, FactoryBot 5.0.2, Rspec 3.8. I've tried the different ways to define an enum. I don't know what to do more.
Model:
class CollegeWhitelist < ApplicationRecord
enum status: {pending: 0, approved: 1, rejected: 2}
has_many :users
has_many :colleges
end
Factory:
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
I was expecting it to pass the test just printing the object at first.
Upvotes: 1
Views: 1168
Reputation: 33430
It's a matter of naming clashing.
You must wrap the value of the status
column within curly brackets otherwise it'll call itself:
FactoryBot.define do
factory :college_whitelist do
...
trait :pending do
status { :pending }
end
trait :approved do
status { :approved }
end
trait :rejected do
status { :rejected }
end
end
end
Upvotes: 1