Need Help with Specs failing when using ActionMailer

No one was able to help me with the last post so I thought I would try
again with more details. I am having problems with specs that invoke
the mailer. My User model has a method that invokes a mailer as part
of the create_from_signup method.

First an example of the test that passes in my spec/models/user_spec
when run in isolation (i.e. spec spec/models/user_spec.rb).

before do
  UserNotifier.deliveries = []
end

it "should signup a valid user and send an activation e-mail" do
  u = User.create_from_signup(params)
  u.state.should == 'pending'
  UserNotifier.should have(1).deliveries
  mail = UserNotifier.deliveries.first
  mail.to.should eql( [u.email] )
  mail.subject.should eql( "#{u.login}, Please activate your new

account" )
end

The spec passes with no problems [ all green :slight_smile: ]

When I run this same spec within my entire suite (spec spec), it fails
with the following backtrace

ArgumentError in 'User Signup - should signup a valid user and send an
activation e-mail"
wrong number of arguments (0 for 1)
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:551:in ‘content_type’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:551:in ‘render_message’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:493:in ‘create!’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:452:in ‘initialize’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:395:in ‘new’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:395:in ‘method_missing’
rails/app/models/user.rb:150:in ‘create_from_signup’

I get this same error in my user controller tests, whether I run it
standalone or within the suite.

Does anyone have any idea what could be causing this?

Thanks in advance

Brad

On Wed, Dec 9, 2009 at 7:48 PM, Brad [email protected]
wrote:

end

ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:551:in ‘render_message’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:493:in ‘create!’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:452:in ‘initialize’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:395:in ‘new’
ruby/lib/ruby/gems/1.8/gems/actionmailer-2.3.5/lib/action_mailer/
base.rb:395:in ‘method_missing’
rails/app/models/user.rb:150:in ‘create_from_signup’

What’s on line 150 in user.rb? Please post the entire method
surrounding that line.

Dave
Thanks for looking at this. I really appreciate it.

Here is the method being called:

144 # Sign up a new user with a default role of USER and with a state
of pending.
145 # The user must activate their account via e-mail before they can
login
146 def self.create_from_signup(params = {} )
147 user = new(params)
148 if user.valid?
149 user.register!
150 UserNotifier.deliver_signup_notification(user)
151 end
152 return user
153 end

Line 150 is calling UserNotifier

class UserNotifier < ActionMailer::Base
def signup_notification(user)
setup_email(user)
@subject += “#{user.login}, Please activate your new account”
@body[:url] = “http://#{SITE_HOST}/activate/#
{user.activation_code}”
end

def password_reset_notification(user, newpassword)
setup_email(user)
@subject += “#{user.login}, Account changes”
@body[:password] = newpassword
end

protected
def setup_email(user)
@recipients = “#{user.email}”
@from = SITE
@subject = "[CONFIRMATION] "
@sent_on = Time.now
@body[:user] = user
end
end

Everything works well in the standalone model spec. It’s when I
combine it with controller spec’s that the problem occurs.

Brad

Finally solved the problem. Even though the application is Rails
2.3.5 it was started on a much earlier version and the view files were
using the old rhtml extensions. The newer auto format detection uses
html.erb, or in the case of the mailer, likes
file_name.text.html.erb. All I had to do was rename the view files
and the specifications passed. Not sure why the application ran fine
with the old file name extensions whereas controller spec’s failed,
but at least it is now working.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs