ruby-on-rails-3 – 使用rails / rspec / capybara / devise测试“帐户确认”

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – 使用rails / rspec / capybara / devise测试“帐户确认”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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

这是我的助手:

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

生成的HTML电子邮件中确认链接.能够做这样的事情(假设“确认我的帐户”)是帐户验证的链接将是可爱的.

last_email.body.find_link("Confirm My Account").click_link

有没有人有任何建议能够识别可以进入request_spec的电子邮件中的链接

谢谢

解决方法

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

我还没有充分探索上面提到的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

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

def last_email
  ActionMailer::Base.deliveries.last
end

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

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

猜你在找的Ruby相关文章