mercredi 7 décembre 2016

RSpec and FactoryGirl

I currently have an RSpec test:

    Rspec.describe Some::ServiceClass do 
      args = ['1', '2']
      subject { described_class.new(args) }

      describe '#finder_method' do 
        let(:user_1) { FactorGirl.create(:user) 

        it 'returns the right number of users' do
          expect(subject.finder_method.count).to eq(1)
        end
      end
    end 

This is what the class under test looks like

   module Some

      class Service

        ....
        # other methods used to identify the resource model

        def finder_method 
          resource.all
        end
      end

    end

Expectation:

  • when resource.all is called, I expected that
  • let(:user_1) FactoryGirl.create(:user), in spec test, would populated 1 row in the users table (test env)
  • therefore when subject.finder_method is evaluated, it would return one AR object.

Actual Result:

  • instead subject.finder_method, which simply calls all on AR object, returns #<ActiveRecord::Relation []>

Can anyone please explain why is this happening? Where is the user_1 that was created by FactoryGirl?

Aucun commentaire:

Enregistrer un commentaire