Rails/Rspec: Testing delayed_job mails

Viewed 14214

Just wondering how to test that actionmailer requests are actually sent to the delayed_job que in rspec.

I would have assumed it was quite simple, but my delayed_job queue doesn't seem to be incrementing. Code below:

Controller:

  def create
    @contact = Contact.new(params[:contact])
      if @contact.save
        contactmailer = ContactMailer
        contactmailer.delay.contact_message(@contact)
        redirect_to(contacts_url)
      else
        render :action => "new"
      end

Spec:

  it "queues mail when a contact is created" do
    expectedcount = Delayed::Job.count + 1
    Contact.stub(:new).with(mock_contact()) { mock_contact(:save => true) }
    post :create, :contact => mock_contact
    expectedcount.should eq(Delayed::Job.count)
  end

Both before and after the call to the controller, the Delayed::Job.count returns 0. I've tried taking the conditional out of the controller, but I still can't get the delayed job count to increment.

Any suggestions appreciated - cheer

5 Answers
Related