brandonhilkert / sucker_punch

Sucker Punch is a Ruby asynchronous processing library using concurrent-ruby, heavily influenced by Sidekiq and girl_friday.
MIT License
2.64k stars 114 forks source link

testing problem #81

Closed ShPakvel closed 10 years ago

ShPakvel commented 10 years ago

Hi, Brandon! I have problem with testing job. I use database_cleaner and follow your instruction from cleaning-test-data-transactions. But when I execute spec I have strange result. Data prepared before job code execution is not reachable from database. It seems like transaction/threads issue or I don't know what it is. Can you help me to solve this? Here is example code

# spec/spec_helper.rb
RSpec.configure do |config|
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  # Clean up all jobs specs with truncation
  config.before(:each, job: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

# spec/jobs/some_job_spec.rb
require 'spec_helper'

describe SomeJob, job: true do
  describe "#perform" do
    before { FactoryGirl.create(:user) }

    it "delivers an email" do
      expect {
        puts User.count # => 1
        SomeJob.new.perform
        puts User.count # => 1
      }.to change{ something }.by(1)
    end
  end
end

# app/jobs/some_job_spec.rb
class SomeJob
  include SuckerPunch::Job

  def perform
    puts User.count # => 0
  end
end
brandonhilkert commented 10 years ago

I don't really understand what the job is supposed to be doing. Are you testing to see if database cleaner works properly?

Output of the log/test.log would be more helpful for debugging this.

brandonhilkert commented 10 years ago

Also, what version of Rspec are you using?

ShPakvel commented 10 years ago

It is just example for simplisity. BTW, I've corrected it (there should be before block instead of let). My Rspec version from Gemfile.lock

    rspec-core (3.0.4)
      rspec-support (~> 3.0.0)
    rspec-expectations (3.0.4)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.0.0)
    rspec-mocks (3.0.4)
      rspec-support (~> 3.0.0)
    rspec-rails (3.0.2)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 3.0.0)
      rspec-expectations (~> 3.0.0)
      rspec-mocks (~> 3.0.0)
      rspec-support (~> 3.0.0)
ShPakvel commented 10 years ago

The main problem for me right now is that code inside my job do not see any record in database created in spec before block.

brandonhilkert commented 10 years ago

If you give me a github URL of the app, I can take a look. Also, the test.log output for the job would help.

ShPakvel commented 10 years ago

@brandonhilkert I've figured out what was the problem. In my rails_helper.rb file (could be spec_helper.rb) there was not comment line with transactional fixtures.

config.use_transactional_fixtures = true

It explains everything. And after comment it the job code works properly (see data in database created in spec). I don't know how it's happened (how I forgot to comment it). :blush: Silly me. But maybe someone will have the same problem. So maybe it would be nice to have note about that case in readme. What do you think about it?

Thanks for your support!

brandonhilkert commented 10 years ago

Makes sense. Can you send a PR?

On Tuesday, October 28, 2014, Pavel Shpak notifications@github.com wrote:

@brandonhilkert https://github.com/brandonhilkert I've figured out what was the problem. In my rails_helper.rb file (could be spec_helper.rb) there was not comment line with transactional fixtures.

config.use_transactional_fixtures = true

It explains everything. And after comment it the job code works properly (see data in database created in spec). I don't know how it's happened (how I forgot to comment it). [image: :blush:] Silly me. But maybe someone will have the same problem. So maybe it would be nice to have note about that case in readme. What do you think about it?

Thanks for your support!

— Reply to this email directly or view it on GitHub https://github.com/brandonhilkert/sucker_punch/issues/81#issuecomment-60783459 .


_The Build a Ruby Gem guide is available! http://brandonhilkert.com/books/build-a-ruby-gem/?utm_source=gmail-sig&utm_medium=email&utm_campaign=gem-config_

http://brandonhilkert.com

ShPakvel commented 10 years ago

Ok, I will send it a couple hours later...