Monday 15 March 2010

ruby on rails - ActionMailer fails when testing mailer -



ruby on rails - ActionMailer fails when testing mailer -

rails 4.1.4 , rspec 3

i'm doing basic email test rspec. if phone call mailer rails console, works perfectly. if phone call mailer spec get:

wrong number of arguments (0 1..2)

the mailer basic:

def create_team_invite(org, email) @organization = org mail(:to=>email, :subject=>'test subject') end

the test pretty basic too:

it 'can send out emails invite others create teams' usermailer.create_team_invite(@org, 'test@test.com').deliver expect(actionmailer::base.deliveries.count).to eq 1 mail service = actionmailer::base.deliveries.first expect(mail.subject).to eq 'test subject' expect(mail.from).to eq 'test@test.com' end

its failing in "mail(:to..." line in mailer. seems maybe configuration issue in environment, have test setup same dev, using smtp , sending mailcatcher port. caught exception , looked @ backtrace, don't see unusual...

anyone seen before?

update: providing additional info requested.

my test.rb, minus comments:

rails.application.configure config.cache_classes = true config.eager_load = false config.serve_static_assets = true config.static_cache_control = 'public, max-age=3600' config.consider_all_requests_local = true config.action_controller.perform_caching = false config.action_dispatch.show_exceptions = false config.action_controller.allow_forgery_protection = false config.action_mailer.delivery_method = :smtp config.action_mailer.perform_deliveries = true config.active_support.deprecation = :stderr config.action_mailer.default_url_options = { :host => 'lvh.me:3000', :only_path=>false } end

the entire rspec failure is:

usermailer

team joining email can send out emails invite others create teams (failed - 1)

failures:

1) usermailer team joining email can send out emails invite others create teams failure/error: usermailer.create_team_invite(@team, 'test@test.com').deliver argumenterror: wrong number of arguments (0 1..2) # ./app/mailers/user_mailer.rb:11:in `create_team_invite' # ./spec/mailers/user_mailer_spec.rb:36:in `block (3 levels) in <top (required)>' finished in 0.25858 seconds (files took 29 minutes 48 seconds load) 1 example, 1 failure

the way configure email via initializer loads email.yml file config, per environment. exact same process used both test , dev, same settings. (again, i'm sending mailcatcher, instead of mail_delivery :test)

update 2

i have narrowed downwards mailer missing "request" object. if dig through error occurring (abstractcontroller rendering.rb, line 109) tries reference request object:

if defined?(request) && request && request.variant.present?

this calling on rack test.rb line 121:

def request(uri, env = {}, &block) env = env_for(uri, env) process_request(uri, env, &block) end

so rack test.rb class beingness seen request method in abstractcontroller... dont know how, or why, or why happening in particular project...

i same exact error, , seems it's trying phone call rack-test request code, when i'm trying test mailer object. did not include stuff set in spec_helper mailer spec. fixed problem me.

# require 'spec_helper' -> removed line, # , manually require files that's needed test mailer object: require 'rubygems' require './config/environment' require './app/mailers/your_mailer'

note: i'm doing rack project using action_mailer, no rails stuff. solution different, idea.

update:

after doing more troubleshooting. found problem in spec_helper.rb file

rspec.configure |config| config.include include rackspechelper # notice double include ... end # rackspechelper custom module module rackspechelper include rack::test::methods # bunch of other helper methods end

notice double include on line

config.include include rackspechelper

at first tried removing line , mailer test running fine. suspicious double include, remove include it's this

config.include rackspechelper

now mailer test runs fine without having manual require posted before above (and can run other test uses rack test stuff).

the double include doing

config.include(include(rackspechelper))

which include rackspechelper in configure block, loads methods in top level namespace! (very bad thing). works, means methods rack::test::methods in global namespace.

so i'm guessing in case, might have line in spec_helper include rack::test::methods in global namespace this

include rack::test::methods

remove , instead set in rspec config this

rspec.configure |config| config.include rackspechelper end

ruby-on-rails ruby rspec actionmailer

No comments:

Post a Comment