Erik Jacobs
Erik Jacobs

Reputation: 901

rspec spec fails only when run in entire test suite - passes when run individually

I have an almost meaningless worker test spec:

require 'rails_helper'
require 'sidekiq/testing'
Sidekiq::Testing.fake!

RSpec.describe FetchArticleContentWorker, type: :worker do

  describe "Sidekiq Worker" do

    before(:each) do
      allow_any_instance_of(DataSource).to receive(:subscribe).and_return(true)
      allow_any_instance_of(FetchArticleContentWorker).to receive(:perform).and_return(true)
    end
    let(:user) { FactoryBot.create(:user) }
    let(:data_source) { FactoryBot.create(:data_source) }
    let(:article) { FactoryBot.create(:article, data_source_id: data_source.id) }

    it "should respond to #perform" do
      expect(FetchArticleContentWorker.new).to respond_to(:perform)
    end

    describe "fetch article content worker" do

      before do
        Sidekiq::Worker.clear_all
      end

      it "increase the worker job size" do
        expect { FetchArticleContentWorker.perform_async(article.url) }.to change(FetchArticleContentWorker.jobs, :size).by(1)
      end

      it "assert that jobs were pushed on to the queue" do
        assert_equal 0, FetchArticleContentWorker.jobs.size
        FetchArticleContentWorker.perform_async(article.url)
        assert_equal 1, FetchArticleContentWorker.jobs.size
      end

      it "job runs successfully" do
        expect(FetchArticleContentWorker.new.perform(article.url)).to eq(true)
      end
    end
  end
end

For whatever reason, this set of specs has suddenly started failing but only when run as a part of the entire test suite. If I run this test directly by itself, it passes every time.

When run with bundle exec rspec (to do the whole suite), I get the following failures for this particular test:

Failures:                                                                      
                                                                               
  1) FetchArticleContentWorker Sidekiq Worker fetch article content worker increase the worker job size
     Failure/Error: expect { FetchArticleContentWorker.perform_async(article.url) }.to change(FetchArticleContentWorker.jobs, :size).by(1)
       expected `Array#size` to have changed by 1, but was changed by 0                
     # ./spec/workers/fetch_article_content_worker_spec.rb:29:in `block (4 levels) in <top (required)>'
     # ./spec/rails_helper.rb:48:in `block (3 levels) in <top (required)>'
     # /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/generic/base.rb:16:in `cleaning'
     # /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:87:in `block (2 levels) in cleaning'
     # /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:88:in `cleaning'
     # ./spec/rails_helper.rb:47:in `block (2 levels) in <top (required)>'

  2) FetchArticleContentWorker Sidekiq Worker fetch article content worker assert that jobs were pushed on to the queue
     Failure/Error: assert_equal 1, FetchArticleContentWorker.jobs.size

     Minitest::Assertion:
       Expected: 1
         Actual: 0
     # /usr/local/bundle/gems/minitest-5.14.0/lib/minitest/assertions.rb:183:in `assert'
     # /usr/local/bundle/gems/minitest-5.14.0/lib/minitest/assertions.rb:218:in `assert_equal'
     # ./spec/workers/fetch_article_content_worker_spec.rb:35:in `block (4 levels) in <top (required)>'
     # ./spec/rails_helper.rb:48:in `block (3 levels) in <top (required)>'
     # /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/generic/base.rb:16:in `cleaning'
     # /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:87:in `block (2 levels) in cleaning'
     # /usr/local/bundle/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:88:in `cleaning'
     # ./spec/rails_helper.rb:47:in `block (2 levels) in <top (required)>'

It appears that these worker specs only fail when a feature using turnip is previously run.

Upvotes: 3

Views: 1012

Answers (1)

Erik Jacobs
Erik Jacobs

Reputation: 901

I ended up finding what I think is the problem.

In my turnip steps file I was setting sidekick to inline:

  # set sidekiq to inline
  require 'sidekiq/testing'
  Sidekiq::Testing.inline!

And, even though I was setting Sidekiq::Testing.fake! in the above mentioned spec file, it didn't seem to "take".

When I moved fake! into the before block, that seemed to fix the problem:

  describe "Sidekiq Worker" do

    before(:each) do
      allow_any_instance_of(DataSource).to receive(:subscribe).and_return(true)
      allow_any_instance_of(FetchArticleContentWorker).to receive(:perform).and_return(true)
      Sidekiq::Testing.fake!
    end

...

I am not sure why this is the fix, but this fixed it.

Upvotes: 2

Related Questions