使用rails/rspec/capybara/devise测试"帐户确认"

Aga*_*oom 13 rspec devise capybara ruby-on-rails-3

我正在使用rspec/capybara/devise在应用程序中进行集成测试.应用程序的一个功能是使用确认功能进行不断填充的"帐户注册"(即注册 - 获取确认电子邮件 - 单击链接 - 帐户已经过验证).

require 'spec_helper'

describe "User Authentication" do
  describe "New user" do
    before(:each) do
      @user = Factory.build(:user)
    end

    it "can confirm account by clicking on confirmation link" do
      visit root_path
      click_link "Register"
      page.should have_content "Register for an account"
      fill_in "user_email", :with => @user.email
      fill_in "user_password", :with => @user.password
      fill_in "user_password_confirmation", :with => @user.password
      fill_in "user_first_name", :with => @user.first_name
      fill_in "user_last_name", :with => @user.last_name
      fill_in "user_city", :with => @user.city
      fill_in "user_province", :with => @user.province
      fill_in "user_country", :with => @user.country
      fill_in "user_expertise", :with => @user.expertise
      choose "user_experience_professional"
      click_button "Go!"
      last_email.to.should include(@user.email)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我的助手:

module MailerMacros
  def last_email
    ActionMailer::Base.deliveries.last
  end
end
Run Code Online (Sandbox Code Playgroud)

确认链接位于生成的HTML电子邮件中.能够做这样的事情(假设"确认我的账户")是账户验证的链接会很可爱.

last_email.body.find_link("Confirm My Account").click_link
Run Code Online (Sandbox Code Playgroud)

有没有人能够识别可能进入request_spec的电子邮件中的链接?

谢谢

DaK*_*KaZ 17

我意识到这个问题已经有一年了,但它指出了一个解决方案,其他人可能会觉得有帮助,所以我想我会发布它.

我没有完全探索email_spec上面提到的宝石,所以这可能是一个更优雅的方法,但这对我的情况非常有效.在我的应用程序中,我们修改了设计以允许用户仅使用电子邮件地址注册,然后他们必须确认其帐户并提示其他信息.所以我需要测试确认是否有效.

我的功能文件如下所示:

  Scenario: Confirm new account
    Given I have registered for an account as "doe@nowhere.com"
    When I follow the confirmation link in the confirmation email
    Then I should be able to set a password
Run Code Online (Sandbox Code Playgroud)

首先,我将last_email帮助器添加到我的黄瓜支持目录中mailer_helper.rb:

def last_email
  ActionMailer::Base.deliveries.last
end
Run Code Online (Sandbox Code Playgroud)

其次,我写了这样的步骤定义:

Given(/^I have registered for an account as "(.*?)"$/) do |user_email|
  visit root_path
  click_link 'register'
  fill_in('Email', with: user_email)
  click_button 'Sign up'
  user = User.find_by_email(user_email)
  user.should_not be_nil
  user.confirmation_token.should_not be_nil
end

When(/^I follow the confirmation link in the confirmation email$/) do
  ctoken = last_email.body.match(/confirmation_token=\w*/)
  visit "/users/confirmation?#{ctoken}"
end

Then(/^I should be able to set a password$/) do
  fill_in('user[password]', with: 'passw0rd')
  fill_in('user[password_confirmation]', with: 'passw0rd')
  fill_in('user[first_name]', with: 'John')
  fill_in('user[last_name]', with: 'Doe')
  click_button 'Activate'
  page.should have_content('Your account was successfully confirmed. You are now signed in.')
  page.should have_css('div#flash_notice.alert.alert-success')
end
Run Code Online (Sandbox Code Playgroud)


Pri*_*kaK 5

你可以简单地做到这一点使用rspec,capybara,gem 'email_spec'`gem 'action_mailer_cache_delivery'.

click_button "Go!"
email = open_email(@user.email)
email.should deliver_to(@user.email)
click_link "Confirm My Account"
sleep 2
Run Code Online (Sandbox Code Playgroud)


soc*_*ata 4

“确认我的帐户”链接是自动生成的,通常基于保存到数据库的某种令牌。

您可以断言该用户的令牌包含在已发送给该用户的确认链接中。或者您可以从邮件中获取该链接,并使用visitrspec 附带的辅助方法。然后检查用户是否被确认。