Testing with Resque with Rspec Examples?

I process my background jobs with 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 check in synchronously, I added

 Resque.inline = Rails.env.test? 

in config / initializers / resque.rb file

Was it supposed that the #perform method call was built in without queue in Redis and without any Resque callbacks like Rails.env.test? returns true in a test environment.

But

 "this must get printed in console" 

never printed during testing. and my tests also fail.

Are there any configurations that I missed. I am currently using

 resque (1.17.1) resque_spec (0.7.0) resque_unit (0.4.0) 
+7
source share
2 answers

I personally check my employees different. I use RSpec and, for example, in my user model, I am testing 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 the following contents:

 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 have no dependency between the model / controller and your worker in your tests. You also should not scoff at things that are not related to the employee.

But if you don’t do it, as you mentioned, it worked for me several times ago. I put Resque.inline = true in the configuration of the test environment.

+8
source

It seems that the registration question has not received an answer. I came across something similar to this, and due to the fact that I did not create a Resque recorder. You can do something simple:

 Resque.logger = Rails.logger 

Or you can configure a separate log file by adding it to /lib/tasks/resque.rake. When you start your worker, he 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!" } 

A mocker like the daniel-spangenberg mentioned above should write to STDOUT if your methods are not in the "private" section of your class. This caused a couple of times when writing rspec tests. ActionMailer also requires custom logging. I guess I expected more agreement than the settings. :)

0
source

All Articles