Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing using Resque with Rspec examples?

I am processing my background jobs using Resque. My model looks like this

class SomeClass
  ...
  repo = Repo.find(params[:repo_id])
  Resque.enqueue(ReopCleaner, repo.id)
  ...
end

class RepoCleaner
  @queue = :repo_cleaner

  def self.perform(repo_id)
    puts "this must get printed in console"
    repo = Repo.find(repo_id)    
    # some more action here
  end
end

Now to test in synchronously i have added

Resque.inline = Rails.env.test?

in my config/initializers/resque.rb file

This was supposed to call #perform method inline without queuing it into Redis and without any Resque callbacks as Rails.env.test? returns true in test environment.

But

"this must get printed in console"

is never printed while testing. and my tests are also failing.

Is there any configurations that i have missed. Currently i am using

resque (1.17.1)
resque_spec (0.7.0)
resque_unit (0.4.0)
like image 693
Gagan Avatar asked Aug 27 '11 02:08

Gagan


1 Answers

I personally test my workers different. I use RSpec and for example in my user model I test something like this:

it "enqueue FooWorker#create_user" do
  mock(Resque).enqueue(FooWorker, :create_user, user.id)
  user.create_on_foo
end

Then I have a file called spec/workers/foo_worker_spec.rb with following content:

require 'spec_helper'

describe FooWorker do

  describe "#perform" do
    it "redirects to passed action" do
      ...
      FooWorker.perform
      ...
    end
  end

end

Then your model/controller tests run faster and you don't have the dependency between model/controller and your worker in your tests. You also don't have to mock so much things in specs which don't have to do with the worker.

But if you wan't to do it like you mentioned, it worked for me some times ago. I put Resque.inline = true into my test environment config.

like image 101
Daniel Spangenberg Avatar answered Sep 21 '22 03:09

Daniel Spangenberg