3

I've running some Cucumber/Capybara tests. I've been using the email_spec gem to check email stuff. Some steps where of the kind 'And "someone@email.com" should receive an email'. They give no problem when I run the test using the rack_test driver. However, they fail when using the selenium driver:

And "someone@email.com" should receive an email                                                                
  expected: 1
       got: 0 (using ==) (RSpec::Expectations::ExpectationNotMetError)

Can you help me? Thanks

sauronnikko
  • 4,665
  • 5
  • 31
  • 47

1 Answers1

2

You have to put your emails into a file because by default rails in test environemnt saves them in static variable that cannot be accessed from test thread. If you're using rails3 set delivery method to :file in cucumber environment. If you're on rails 2.x put this into your cucumber initializer:

ActionMailer::Base.class_eval do
  DELIVERIES_CACHE_PATH =
    File.join(RAILS_ROOT,'tmp','cache',"action_mailer_cache_deliveries {ENV['TEST_ENV_NUMBER']}.cache")

  def perform_delivery_cache(mail)
    deliveries = File.open(DELIVERIES_CACHE_PATH, 'r') do |f|
      Marshal.load(f)
    end

    deliveries << mail
    File.open(DELIVERIES_CACHE_PATH,'w') { |f| Marshal.dump(deliveries, f) }
  end

  def self.cached_deliveries
    File.open(DELIVERIES_CACHE_PATH,'r') { |f| Marshal.load(f) }
  end
end

config.action_mailer.delivery_method = :cache
iafonov
  • 5,152
  • 1
  • 25
  • 21
  • Hi. I've put `config.action_mailer.delivery_method = :file` in my cucumber enviroment and the tests still fail. Is there something else I should have done? – sauronnikko Nov 23 '11 at 15:32
  • I've read that setting it to `:file` saves the emails in the `tmp/mails` folder. Just after sending the email (which still is being sent because I can see it in the server log), I debug and I can see that not only there weren't any files there, but the folder itself is missing. Besides, when I try my rack_test tests with that delivery method, they also fail (they didn't fail before). What's going on? – sauronnikko Nov 23 '11 at 22:28