我正在使用 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
这是我的 helper :
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
上面提到的 gem 可能是一种更优雅的方法,但这对我的情况非常有效。在我的应用程序中,我们已设计修改为允许用户仅使用电子邮件地址进行注册,然后他们必须确认他们的帐户并提示输入其他信息。所以我需要测试确认是否有效。
我的功能文件如下所示:
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
我的 cucumber 支持目录的助手为 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-on-rails-3 - 使用 rails/rspec/capybara/devise 测试 "Account Confirmation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8886748/