leshill / resque_spec

RSpec matcher for Resque
MIT License
295 stars 86 forks source link

Load resque_scheduler #117

Open mrvovanness opened 9 years ago

mrvovanness commented 9 years ago

I am trying to test dynamic schedules. As is suggested I included in my Gemfile:

gem 'resque-scheduler', require: 'resque_scheduler'

but I get error can not load such file -- resque_scheduler. I tried to include in environments/test.rb, but with same effect. I tried also to write simple test to be sure that gem is working:

describe Survey do
  let!(:survey) { create(:survey) } 
  before do
    ResqueSpec.reset!
  end

  it 'works' do
    survey.update(title: 'Good')
    expect(SendEmailsJob).to have_schedule_size_of(1)
  end
end

update action triggers scheduling(via after_save callback)

And I had output:

ExpectationNotMetError: expected that SendEmailsJob would have 1 s
cheduled entries, but got 0 instead>>

And web interface of Resque shows me that I created new schedule. I need some clues what is going on.

leshill commented 9 years ago

Hi @mrvovanness,

Try removing the require option from your Gemfile. The latest README of resque-scheduler indicates that it is not needed. If that works, ping back and I will update the _resquespec README.

mrvovanness commented 9 years ago

Hello @leshill!

I removed the require option and that works. But whenever I trigger scheduling in my test it shows me that my schedule size is 0, but actually in development and production mode scheduling works perfect. For example, when I create new instance of `Survey' this triggers the creation of new schedule via callback:

#...
after_save :schedule_send_emails
def schedule_send_emails
  config[:every] = '1w'
  name = "send_emails_for_survey_#{id}"
  config[:class] = 'SendEmailsJob'
  config[:queue] = 'send_emails'
  config[:persist] = true
  config[:args] = id
  Resque.set_schedule(name, config)
end

but when I test this:

it 'create schedule on #save' do
    survey.save
    expect(SendEmailsJob).to have_schedule_size_of(1)
  end

I have error expected that SendEmailsJob would have 1 s cheduled entries, but got 0 instead I have no idea what I am doing wrong For now I just end up requesting test Redis database:

  it 'create schedule on #save' do
    survey.save
    expect(Resque.schedule_persisted?("send_emails_for_survey_#{survey.id}")).to be true
  end
leshill commented 9 years ago

Hi @mrvovanness,

The code is not covering that method from the resque-scheduler API:

Resque.set_schedule(name, config)

This is not supported yet. A PR to do so would be welcome.