0

我正在关注Ryan Bates 的 #275 Railscastslast_email ,但由于该方法,我无法通过测试。

这是测试:spec/requests/password_resets_spec.rb

require 'spec_helper'

subject { page }

describe "Password reset for a registered user" do

  let(:user) { FactoryGirl.create(:user) }  
  before do
    visit new_password_reset_path
    fill_in t('user.email'), with: user.email
    click_button(t('submit.reset.password'))
  end

  it { should have_selector('div.alert.alert-success', t('flash.success.reset_password.sent')) }
  expect(last_email).to have_content(user.email)
end

这里是错误消息:

spec/requests/password_resets_spec.rb:19:in `block (2 levels) in <top (required)>': undefined local variable or method `last_email' for #<Class:0x000000063b18d8> (NameError

这是new_password_reset视图:

<%= form_tag password_resets_path, :method => :post do %>
  <div class="field">
    <%= label_tag :email, t('user.email') %>
    <%= text_field_tag :email, params[:email] %>
  </div>
  <%= submit_tag t('submit.reset.password'), class: "btn btn-primary" %>
<% end %>

new_password_reset控制器create方法:

def create 
  user = User.find_by_email(params[:email])
  user.send_password_reset if user
  flash[:success] = t('flash.success.reset_password.sent')
  redirect_to root_url
end 

就像在 railscast 中一样,我做了以下事情:

规范/spec_helper.rb

require 'capybara/rspec'

RSpec.configure do |config|
  # ...
  config.include(MailerMacros)
  config.before(:each) { reset_email }
end

规范/支持/mailer_macros.rb

module MailerMacros
  def last_email
    ActionMailer::Base.deliveries.last
  end

  def reset_email
    ActionMailer::Base.deliveries = []
  end
end
4

1 回答 1

0

It's because you're calling the last_email method at the class level rather than within a block.

You need expect(last_email).to have_content(user.email) to be defined with a it block i.e.

it "should show alert and dispatch email to the user" do
   page.should have_selector('div.alert.alert-success', t('flash.success.reset_password.sent'))
   expect(last_email).to have_content(user.email)
end
于 2013-10-01T09:24:02.773 回答