Ruby on Rails教程测试10..20

时间:2016-04-15 03:39:40

标签: ruby-on-rails

有人可以通过Michael Harty的测试10.20 Ruby on Rails教程吗? 我有下面的错误,我无法理解;除了我完全遵循教程指导。

对于前。教程说改为noreply@example.com"]         但它指向这条线的错误...... 我是否应该更改此行并告知Michael Harty有关错误/做其他事情来修复错误?

我使用:ruby-2.3.0 rails - 4.2.2

错误:

  

错误[" test_account_activation",UserMailerTest,2016-03-23 16:43:45   +0000] test_account_activation#UserMailerTest(1458751425.73s)ArgumentError:ArgumentError:参数个数错误(给定   0,预期1)               app / mailers / user_mailer.rb:3:在account_activation' test/mailers/user_mailer_test.rb:6:in块中'           app / mailers / user_mailer.rb:3:在account_activation' test/mailers/user_mailer_test.rb:6:in块中'

     

失败[" test_password_reset",UserMailerTest,2016-03-23 16:43:45   +0000] test_password_reset#UserMailerTest(1458751425.83s)           预期:[" from@example.com"]             实际:[" noreply@example.com"]           test / mailers / user_mailer_test.rb:16:在`block in'

     

39/39:   [================================================= ============] 100%   时间:00:00:03,时间:00:00:03

// user_mailer.rb

    class UserMailer < ApplicationMailer

      def account_activation(user)
        @user = user
        mail to: user.email, subject: "Account activation"
      end

      def password_reset
        @greeting = "Hi"

        mail to: "to@example.org"
      end
    end




//user_mailer_test.rb


require 'test_helper'


class UserMailerTest < ActionMailer::TestCase


  test "account_activation" do


    mail = UserMailer.account_activation


    assert_equal "Account activation", mail.subject


    assert_equal ["to@example.org"], mail.to


    assert_equal ["from@example.com"], mail.from


    assert_match "Hi", mail.body.encoded

  end

  test "password_reset" do

    mail = UserMailer.password_reset

    assert_equal "Password reset", mail.subject

    assert_equal ["to@example.org"], mail.to

    assert_equal ["from@example.com"], mail.from

    assert_match "Hi", mail.body.encoded

  end


end

我添加了将Michael更改为Tatyana的文件,但仍然出现以下错误:我找到此文件并将其添加到更改michael到tatyana但仍然出现如下错误: 错误[&#34; test_account_activation&#34;,UserMailerTest,2016-03-23 16:43:44 +0000]  test_account_activation#UserMailerTest(1458751424.20s) ActionView :: Template :: Error:ActionView :: Template :: Error:缺少要链接的主机!请提供:host参数,设置default_url_options [:host],或将:only_path设置为true             app / views / user_mailer / account_activation.html.erb:9:_app_views_user_mailer_account_activation_html_erb__2380038393264600902_67365800' app/mailers/user_mailer.rb:5:in account_activation&#39;             test / mailers / user_mailer_test.rb:9:in block in <class:UserMailerTest>' app/views/user_mailer/account_activation.html.erb:9:in _ app_views_user_mailer_account_activation_html_erb__2380038393264600902_67365800&#39;         app / mailers / user_mailer.rb:5:account_activation' test/mailers/user_mailer_test.rb:9:in块中&#39;

4 个答案:

答案 0 :(得分:0)

UserMailer.account_activation 方法在测试中需要用户参数。

class UserMailerTest < ActionMailer::TestCase

  test "account_activation" do
    mail = UserMailer.account_activation(User.create(email: "foo@bar.com"))
    assert_equal "Account activation", mail.subject
    assert_equal ["to@example.org"], mail.to
    assert_equal ["from@example.com"], mail.from
    assert_match "Hi", mail.body.encoded
  end
end

答案 1 :(得分:0)

该错误来自user_mailer_test.rb:6第6行。

class UserMailer第3行,您将user对象传递给def account_activation(user),但在UserMailer.account_activation中没有传递任何内容。继续关注那本书应该是这样的例子。

user = users(:michael)
user.activation_token = User.new_token
mail = UserMailer.account_activation(user) # right here user passed.

答案 2 :(得分:0)

test / mailers / user_mailer_test.rb如下所示,可以摆脱一个错误:

要求'test_helper'

类UserMailerTest&lt;的ActionMailer ::测试用例

测试“account_activation”做

user = users(:tatyana)

user.activation_token = User.new_token

mail = UserMailer.account_activation(user)

assert_equal "Account activation", mail.subject

assert_equal [user.email], mail.to

assert_equal ["noreply@example.com"], mail.from

assert_match user.name,               mail.body.encoded

assert_match user.activation_token,   mail.body.encoded

assert_match CGI::escape(user.email), mail.body.encoded

还有另外一个如下: 错误[“test_account_activation”,UserMailerTest,2016-03-23 16:43:47 +0000]  test_account_activation#UserMailerTest(1458751427.01s) ActionView :: Template :: Error:ActionView :: Template :: Error:缺少要链接的主机!请提供:host参数,设置default_url_options [:host],或将:only_path设置为true             app / views / user_mailer / account_activation.html.erb:9:_app_views_user_mailer_account_activation_html_erb__1635017718511086794_82209820' app/mailers/user_mailer.rb:5:in account_activation'             test / mailers / user_mailer_test.rb:9:in block in <class:UserMailerTest>' app/views/user_mailer/account_activation.html.erb:9:in _ app_views_user_mailer_account_activation_html_erb__1635017718511086794_82209820'         app / mailers / user_mailer.rb:5:在{<1}}块中的'

答案 3 :(得分:0)

它已解决。错误是通过不放置电子邮件选项创建的。 感谢