Search code examples
ruby-on-railsrspecfactory-botshoulda

rspec factory girls with shoulda uniqueness test error


Good day, currently i'm struggling with this error.

Failure/Error: FactoryGirl.create(:subscription) NoMethodError: undefined method `attribute_list' for "AdvtCategories":String

i can't find correct solution for this error. what i found was a suggestion that some column is the same name as the class. didn't help my test

 describe Subscription do
  context "validations" do
    #it { should validate_presence_of :category_id }
    it do
      FactoryGirl.create(:category)
      FactoryGirl.create(:subscription)
      should validate_uniqueness_of( :category_id).scoped_to(:user_id) 
    end
   # TODO validator :category_should_exist
  end  
  #it { should belong_to(:user) }
  #it { should belong_to(:category) }
  #specify { Subscription.per_page.should eq(20) }
end

factories:

factory :subscription do
    association :category, factory: :category, :class => 'AdvtCategory'
    user
  end

factory :category do
    name "MyString"
    alias_name "MyString"
    number_of_items 1
    type ""
  end

UPDATE 1 it's a factory_girl error. solving it

UPDATE 2

it's factory_girl error on create (build passes of course, so db error) updated code


Solution

  • Changed from one class to another.

    require 'spec_helper'
    describe Subscription do
      before do
        FactoryGirl.create(:subscription)
      end
    
      context "validations" do
        it { should validate_presence_of :category_id }
        it do
    
          should validate_uniqueness_of( :category_id).scoped_to(:user_id).with_message(/уже создана/) 
        end
      end  
      it { should belong_to(:user) }
      it { should belong_to(:category) }
      specify { Subscription.per_page.should eq(20) }
    end
    
    
      factory :subscription do
        association :category, factory: :advt_category 
        user
      end
      factory :advt_category do
        name 'Category'
        alias_name '1'
      end