未定义的局部变量或方法“last_email”

时间:2013-06-12 13:04:41

标签: rspec devise ruby-on-rails-3.2 capybara

我遇到了如何让我的Devise密码重置测试正常工作的问题,因为我在代码中遇到错误:

require 'spec_helper'

describe "Passwords" do
    describe "Forgot Password" do
        let(:user) { FactoryGirl.create(:user) }
        it 'should reset a users password' do
            reset_mailer
            visit '/'
            click_link 'Sign In'
            fill_in 'Password', :with => 'fakepass'
            fill_in 'Email', :with => user.email
            click_button "Sign in"
            current_path.should == user_session_path
            page.should have_content 'Invalid email or password.'
            click_link 'Forgot your password?'
            current_path.should == new_user_password_path
            page.should have_content 'Forgot Password'
            fill_in 'Email', :with => user.email
            click_button 'Send me password reset instructions'
            current_path.should == user_session_path
            page.should have_content 'You will receive an email'

            last_email.to.should include(user.email)
            open_email(user.email)
            current_email.first(:link, 'Change My password').click

            within('body') do
                page.should have_content 'Change Your Password'
            end

            fill_in "user[password]", :with => "password"
            fill_in "user[password_confirmation]", :with => "password"
            click_button "Change My Password"

            within('body') do
                page.should have_content 'password changed.'
            end

        end
    end
end

比我的错误:

Passwords Forgot Password should reset a users password
     Failure/Error: last_email.to.should include(user.email)
     NameError:
       undefined local variable or method `last_email' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xb788c18>

为什么会出现此错误?我该如何纠正?

更新

features/email_steps.rb

# Commonly used email steps
#
# To add your own steps make a custom_email_steps.rb
# The provided methods are:
#
# last_email_address
# reset_mailer
# open_last_email
# visit_in_email
# unread_emails_for
# mailbox_for
# current_email
# open_email
# read_emails_for
# find_email
#
# General form for email scenarios are:
#   - clear the email queue (done automatically by email_spec)
#   - execute steps that sends an email
#   - check the user received an/no/[0-9] emails
#   - open the email
#   - inspect the email contents
#   - interact with the email (e.g. click links)
#

更新代码:

open_last_email.to.should include(user.email)
open_email(user.email)
current_email.first(:link, 'Change My Password').click

Failure/Error: current_email.first(:link, 'Change My Password').click
     NoMethodError:
       undefined method `first' for #<Mail::Message:0xb560dc8>

1 个答案:

答案 0 :(得分:0)

我能够让它运转起来。问题在于email_steps.rb。我不知道它,因为我正在运行预制的练习应用程序。

关于我遇到问题的代码区:

我的错误代码是:

last_email.to.should include(user.email)
open_email(user.email)
current_email.first(:link, 'Change My password').click

我现在通过测试的新代码:

open_last_email.to.should include(user.email)
open_email(user.email)
click_first_link_in_email