0

当我在代码中遇到错误时,我遇到了如何让我的设计密码重置测试正常工作的问题:

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

2 回答 2

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
于 2013-06-12T14:48:59.393 回答
0

这个对我有用,顺便说一句,我正在使用带有 rspec 的 gem 'capybara-email'

open_email(user.email)
current_email.click_link 'Change my password'
于 2021-11-12T12:58:37.373 回答