0

В прошлом, Rails 3, я интегрировал проверку почтовой программы в действие с моими тестами Cucumber/Rspec-Capybara, как в следующем примере. С Rails 4 использование следующего, похоже, не работает.Тестирование интеграции ActionMailer и ActiveJob

Я могу видеть, что задание поставлено в очередь с помощью enqueued_jobs.size. Как начать задание в очереди, чтобы убедиться, что тема электронной почты, получатель и тело электронной почты правильно созданы?

приложение/контроллеры/robots_controller.rb

class MyController < ApplicationController 
    def create 
    if robot.create robot_params 
     RobotMailer.hello_world(robot.id).deliver_later 
     redirect_to robots_path, notice: 'New robot created' 
    else 
     render :new 
    end 
    end 
end 

спецификации/особенности/robot_spec.rb

feature 'Robots' do 
    scenario 'create a new robot' do 
    login user 
    visit '/robots' 
    click_link 'Add Robot' 
    fill_in 'Name', with: 'Robbie' 
    click_button 'Submit' 

    expect(page).to have_content 'New robot created' 

    new_robot_mail = ActionMailer::Base.deliveries.last 
    expect(new_robot_mail.to) eq '[email protected]' 
    expect(new_robot_mail.subject) eq "You've created a new robot named Robbie" 
    end 
end 

ответ

1

Я использую RSpec-activejob камень и следующий код:

RSpec.configure do |config| 
    config.include(RSpec::ActiveJob) 

    # clean out the queue after each spec 
    config.after(:each) do 
    ActiveJob::Base.queue_adapter.enqueued_jobs = [] 
    ActiveJob::Base.queue_adapter.performed_jobs = [] 
    end 

    config.around :each, perform_enqueued: true do |example| 
    @old_perform_enqueued_jobs = ActiveJob::Base.queue_adapter.perform_enqueued_jobs 
    ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true 
    example.run 
    ActiveJob::Base.queue_adapter.perform_enqueued_jobs = @old_perform_enqueued_jobs 
    end 

    config.around :each, perform_enqueued_at: true do |example| 
    @old_perform_enqueued_at_jobs = ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs 
    ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true 
    example.run 
    ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = @old_perform_enqueued_at_jobs 
    end 
end 

Это позволяет мне отмечать функции/сценарии с perform_enqueued: true, если я хочу, чтобы задания выполнялись фактически.

+0

Спасибо за это! Похоже на код из модуля ActiveJob :: TestHelper. Я просто не смог заставить его работать. FWIW, вам не нужно использовать драгоценный камень rspec-activejob, поскольку он предоставляет только сокеты. Окружение: для каждой конфигурации достаточно запускать задания. – efoo