user1072337
user1072337

Reputation: 12945

NoMethodError: undefined method `deliver_now' for #<Mail::Message:0x000001058258b0> RoR

I am running some tests in my Rails 4 application, and I am receiving the following errors:

1) Error:
UsersLoginTest#test_valid_signup_information:
NoMethodError: undefined method `deliver_now' for #<Mail::Message:0x000001058258b0>
    app/controllers/users_controller.rb:33:in `create'
    test/integration/users_login_test.rb:33:in `block (2 levels) in <class:UsersLoginTest>'
    test/integration/users_login_test.rb:32:in `block in <class:UsersLoginTest>'


  2) Error:
UsersSignupTest#test_valid_signup_information:
NoMethodError: undefined method `deliver_now' for #<Mail::Message:0x0000010b8fa7a8>
    app/controllers/users_controller.rb:33:in `create'
    test/integration/users_signup_test.rb:21:in `block (2 levels) in <class:UsersSignupTest>'
    test/integration/users_signup_test.rb:20:in `block in <class:UsersSignupTest>'

They both have said: undefined method `deliver_now'

I'm not sure why this is the case.

app/controllers/users_controller.rb:33 ->

def create
    @user = User.new(user_params)
    if @user.save
      UserMailer.account_activation(@user).deliver_now
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

test/integration/users_login_test.rb:21 ->

test "valid signup information" do
    get signup_path
    assert_difference 'User.count', 1 do
      post_via_redirect users_path, user: { name:  "Example User",
                                            email: "[email protected]",
                                            password:              "password",
                                            password_confirmation: "password" }
    end
    assert_template 'users/show'
    assert is_logged_in?
  end

Do you see the issue?

Upvotes: 1

Views: 2691

Answers (2)

Varus Septimus
Varus Septimus

Reputation: 1650

deliver_now has been introduced in rails 4.2.x It does not work with rails 4.1.0

Example:

omega@UbuntuSvr14:~/railsprjs/sample_app$ rake test
Started

ERROR["test_valid_signup_information", UsersSignupTest, 1.7315462]
 test_valid_signup_information#UsersSignupTest (1.73s)
NoMethodError:         NoMethodError: undefined method     `deliver_now'         for     #<Mail::Message:0x00000005e8e108>
        app/controllers/users_controller.rb:18:in `create'
        test/integration/users_signup_test.rb:22:in `block (2 levels) in <class:UsersSignupTest>'
        test/integration/users_signup_test.rb:21:in `block in <class:UsersSignupTest>'
    app/controllers/users_controller.rb:18:in `create'
    test/integration/users_signup_test.rb:22:in `block (2 levels) in <class:UsersSignupTest>'
    test/integration/users_signup_test.rb:21:in `block in <class:UsersSignupTest>'

  37/37: [=================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.97998s
37 tests, 155 assertions, 0 failures, 1 errors, 0 skips
omega@UbuntuSvr14:~/railsprjs/sample_app$ rails -v
Rails 4.1.0
omega@UbuntuSvr14:~/railsprjs/sample_app$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
omega@UbuntuSvr14:~/railsprjs/sample_app$ gem -v
2.2.2
omega@UbuntuSvr14:~/railsprjs/sample_app$ gem update --system
Updating rubygems-update
Fetching: rubygems-update-2.4.6.gem (100%)
Successfully installed rubygems-update-2.4.6
(...)
omega@UbuntuSvr14:~/railsprjs/sample_app$ gem -v
2.4.6

Next, we update to rails 4.2.0 (Caution: don't update Rails this way - the simple way - in production!):

omega@UbuntuSvr14:~/railsprjs/sample_app$ vi Gemfile
omega@UbuntuSvr14:~/railsprjs/sample_app$ head Gemfile
source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
gem 'bcrypt','~> 3.1.7'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'faker', '~> 1.4.2'
gem 'will_paginate',           '~>3.0.7'
gem 'bootstrap-will_paginate', '~>0.0.10'

omega@UbuntuSvr14:~/railsprjs/sample_app$ bundle update rails
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.2
(...)
Your bundle is updated!
Gems in the group production were not installed.
omega@UbuntuSvr14:~/railsprjs/sample_app$ rails -v
Rails 4.2.0
omega@UbuntuSvr14:~/railsprjs/sample_app$ rake test

Started

  37/37: [=================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.32801s
37 tests, 156 assertions, **0 failures, 0 errors**, 0 skips
omega@UbuntuSvr14:~/railsprjs/sample_app$

Now it works...

Upvotes: 0

neo
neo

Reputation: 4116

Try:

  UserMailer.account_activation(@user).deliver! # or .deliver

Upvotes: 1

Related Questions