-1

我在Ruby on Rails應用程序中收到2個錯誤。我正在嘗試從Rails教程中完成示例應用程序。Rails教程(M. Hartl) - 我收到兩個錯誤,我需要幫助

這裏是我的錯誤:

1) Error: 
UserMailerTest#test_account_activation: 
ArgumentError: wrong number of arguments (1 for 0) app/mailers/user_mailer.rb:3:in `account_activation'  test/mailers/user_mailer_test.rb:9:in `block in <class:UserMailerTest>' 

2) Failure: 
UserMailerTest#test_password_reset [/home/andy/Ruby_Projects/sam 
ple_app/test/mailers/user_mailer_test.rb:21]: Expected: ["[email protected]"] Actual: ["[email protected]"] 

這裏是我的兩個問題代碼:

require 'test_helper' 

class UserMailerTest < ActionMailer::TestCase 

    test "account_activation" do 
    user = users(:andy) 
    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 ["[email protected]"], 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 
    end 

    test "password_reset" do 
    mail = UserMailer.password_reset 
    assert_equal "Password reset", mail.subject 
    assert_equal ["[email protected]"], mail.to 
    assert_equal ["[email protected]"], mail.from 
    assert_match "Hi", mail.body.encoded 
    end 
end 

這裏是UserMailer.rb:

class UserMailer < ApplicationMailer 

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

    def password_reset 
    @greeting = "Hi" 
    mail to: "[email protected]" 
    end 
end 

我一直在下面該教程使用教程中的內容檢查我的代碼,但仍然無法找到我做錯了什麼。如果您需要更多信息,請告訴我。我會盡力並提供。

+1

您可能會發現[將此問題和答案(http://stackoverflow.com/questions/28614943/rails-tutorial-ch-10-test-10-20-失敗)有幫助。 – lurker

+1

關於發佈的一些反饋:將來,請確保您正確地設置代碼的格式並添加_railstutorial.org_標記。告訴我們你處於哪一章也是有用的,也沒有'UserMailer.rb'這樣的文件。什麼是第一段代碼所在的文件名? –

+0

感謝您的評論,我會牢記這一點。我在第10章。它假設是user_mailer.rb,我只是錯過了錯誤的輸入。抱歉。 – Andy

回答

1

您沒有爲account_activation方法定義參數。您需要添加一個user說法:

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

感謝您的建議,我修復了第一個問題,但仍然無法修復第二個錯誤消息。 – Andy

+0

如果您想覆蓋默認發件人地址,請將以下內容添加爲:from from fromexample.com到郵件列中:and subject :. – infused