Meryl
Meryl

Reputation: 21

Factorybot Rspec Error on factory failing 'update_attribute' on create

My factory: rollouts.rb


FactoryBot.define do
  factory :rollout do
    name { Faker::IndustrySegments.industry.parameterize.underscore }
    percent_enabled { 0 }
    offset { 0 }
    green_list { [] }
    red_list { [] }
    resource_type { 'User' }
    trait :with_user do
      resource_type { 'User' }
    end
  end
end

Model: rollout.rb

class Rollout < ApplicationRecord
  before_create :set_offset
  before_validation :uppercase_resource_type

  validates :name, presence: true, uniqueness: { case_sensitive: false }
  validates_inclusion_of :percent_enabled, in: 0..100
  validates :resource_type, presence: true, inclusion: { in: %w[User Role Instrument Profile] }

 ...methods...
end

Error:

Failures:

  1) Rollout a valid Rollout that has working validations is valid with valid attributes
     [31mFailure/Error: update_attribute(:percent_enabled, num)[0m
     [31m[0m
     [31mSystemStackError:[0m
     [31m  stack level too deep[0m
     [36m# ./app/models/rollout.rb:60:in `percent_enabled='[0m
     [36m# ./app/models/rollout.rb:60:in `percent_enabled='[0m
     [36m# ./app/models/rollout.rb:60:in `percent_enabled='[0m
     [36m# ./app/models/rollout.rb:60:in `percent_enabled='[0m
     [36m# ./app/models/rollout.rb:60:in `percent_enabled='[0m
     [36m# ./app/models/rollout.rb:60:in `percent_enabled='[0m
     [36m# ./app/models/rollout.rb:60:in `percent_enabled='[0m
     [36m# ./app/models/rollout.rb:60:in `percent_enabled='[0m
...

I am trying to run very very basic tests and for some reason I cannot assign percent_enabled, which is an attribute of my model

rollout_spec.rb

RSpec.describe Rollout, type: :model do
  describe 'a valid Rollout' do
    let(:rollout) { create(:rollout, :with_user) }

    context 'that has working validations' do

      it 'is valid with valid attributes' do
        print(rollout)

        expect(rollout).to be_valid
      end
   end
end

if I comment out the percent_enabled { 0 } line in the factory running rspec works just fine

I cant see any obvious typos and am not sure why this problem is persisting. I have run rspec a bunch of times, commenting out different lines and determined that something is wrong with the attribute assignment line in the factory but I cant figure out what.

I tried a fresh bundle update just to see if there was a fix to this that I was behind, but no luck.

Upvotes: 0

Views: 80

Answers (1)

Meryl
Meryl

Reputation: 21

So, the problem ended up being VScode... I had created a percent_enabled= method previously, decided against it, but VScode was having an issue with saving the updated file properly. Still not 100% sure what happened, but its not a problem anymore!

Upvotes: 0

Related Questions