Search code examples
rubysinatraminitestpadrinorack-test

Ruby load module in test


I am running a padrino application and have started with the included mailers. I want to test that a mail is sent and had previously had no trouble accessing the Mail::TestMailer object to look at the mails delivered during the test.

That is the background about what I am doing but not precisely the question. I want to know how can a module become available to the runtime environment. I have this test in two versions

first

def test_mailer
  Mail::TestMailer.deliveries.clear
  get '/owners/test'
  e =  Mail::TestMailer.deliveries.pop
  puts e.to.to_s
end

second

def test_mailer
  get '/owners/test'
  Mail::TestMailer.deliveries.clear
  e =  Mail::TestMailer.deliveries.pop
  puts e.to.to_s
end

In the second version this test fails with the error message NoMethodError: undefined method to' for nil:NilClass This makes sense to me. I clear the messages then ask for the last one which should be nil. However when I run the test on the first version the error is NameError: uninitialized constant OwnersControllerTest::Mail

So somehow the get method is causing the Mail object/module to be made available. I don't understand how it can do this. I don't know if this is a rack-test or padrino thing so am unsure what extra information to copy in here.


Solution

  • Add require 'mail' to your test helper.

    The issue is explained here: https://github.com/padrino/padrino-framework/issues/1797