NoMethodError:未定义的方法`deliver_now'用于#<mail :: message:0x000001058258b0> RoR </mail :: message:0x000001058258b0>

时间:2015-01-06 01:38:25

标签: ruby-on-rails ruby ruby-on-rails-4

我正在我的Rails 4应用程序中运行一些测试,我收到以下错误:

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>'

他们都说过:未定义的方法`deliver_now&#39;

我不确定为什么会这样。

app / controllers / users_controller.rb:33 - &gt;

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 - &gt;

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

你看到了这个问题吗?

2 个答案:

答案 0 :(得分:1)

尝试:

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

答案 1 :(得分:0)

deliver_now已在rails 4.2.x中引入 它不适用于rails 4.1.0

实施例

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

接下来,我们更新到rails 4.2.0(注意:不要以这种方式更新Rails - 简单的方法 - 在生产中!):

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$

现在它有效......