开发者

testing using Resque with Rspec examples?

开发者 https://www.devze.com 2023-03-31 16:44 出处:网络
I am processing my background jobs using Resque. My model looks like开发者_如何转开发 this class SomeClass

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)


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.


It looks like the question about logging never got answered. I ran into something similar to this and it was from not setting up the Resque logger. You can do something as simple as:

Resque.logger = Rails.logger

Or you can setup a separate log file by adding this to your /lib/tasks/resque.rake. When you run your worker it will write to /log/resque.log

Resque.before_fork = Proc.new {
ActiveRecord::Base.establish_connection

# Open the new separate log file
logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')

# Activate file synchronization
logfile.sync = true

# Create a new buffered logger
Resque.logger = ActiveSupport::Logger.new(logfile)
Resque.logger.level = Logger::INFO
Resque.logger.info "Resque Logger Initialized!"
}

Mocking like daniel-spangenberg mentioned above ought to write to STDOUT unless your methods are in the "private" section of your class. That's tripped me up a couple times when writing rspec tests. ActionMailer requires it's own log setup too. I guess I've been expecting more convention than configuration. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消