2012-01-16 63 views
12

我使用的RSpec /豚/設計中的應用進行集成測試。該應用程序的功能之一是使用確認功能(即註冊 - 獲取確認電子郵件 - 點擊鏈接 - 帳戶經過驗證)的不斷填充的「帳戶註冊」。測試「賬戶確認書」與軌/ rspec的/豚/設計

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?

感謝

回答

4

「確認我的帳戶」是自動生成的鏈接,通常立足於某種被保存到數據庫中的令牌。

您可以斷言,對於該用戶的令牌包含在已發送給用戶的確認鏈接。或者,您可以從郵件中獲取該鏈接,並使用rspec附帶的幫助程序方法visit。然後檢查用戶是否確認。

6

你可以簡單地做到這一點使用rspeccapybaragem '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 
+0

剛剛意識到這是一個歲的問題。 – PriyankaK 2013-11-06 16:51:08

+1

仍幫我。把我放到email_spec寶石上,這真是太棒了!您也可以使用email_spec中的'click_first_link_in_email電子郵件' – mattvv 2014-03-16 00:54:08

+0

@mattvv我嘗試過'''click_first_link_in_email''',但沒有爲我工作。感謝您的回覆! – PriyankaK 2014-03-18 19:11:04

1

與@socjopata描述的方法類似。

def extract_token_from_email(token_name) 
    mail_body = last_email.body.to_s 
    mail_body[/#{token_name.to_s}_token=([^"]+)/, 1] 
end 

,然後通過使用:

visit whatever_path({ some_token: token_name }) 

我通過在規格/支持/ *文件,將提取由色器件生成的令牌中定義一個幫手做從GitHub的要點偷取代碼,但無法找到它的參考,所以我不能完全信任。

16

我意識到這個問題是一歲,但它指出了我對別人有幫助的,所以我想我會反正它張貼的解決方案。

我還沒有充分挖掘的上方,這可能是一個更優雅的方式提到email_spec寶石,但是這個工作得很好了我的情況。在我的應用程序中,我們設計了修改以允許用戶僅使用電子郵件地址註冊,然後他們必須確認其帳戶並提示您提供更多信息。所以我需要測試確認是否有效。

我的特徵文件看起來像這樣:

Scenario: Confirm new account 
    Given I have registered for an account as "[email protected]" 
    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 "https://stackoverflow.com/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 
+0

它可能只是爲我,但我認爲確認的URL應該是「/用戶/ ...」,雖然我知道設計提供了一個confirmation_url幫手,我無法工作。 – 2014-07-01 17:20:03

+1

爲了使它工作,我必須做email.body.parts [0] .body.decoded.match(/ confirmation_token = [^ \ n] * /)在我的情況下,我發送一個多部分郵件,所以我提取令牌文本版本,我知道我已經在鏈接後加了一個換行符。 – 2014-11-11 12:00:21

+0

在'rspec'中,我使用'confirmation_token = $ 1如果ActionMailer :: Base.deliveries.last.body.raw_source =〜/ confirmation_token =([^'「] +)/' – ghayes 2014-12-16 00:11:05

3

如果您需要在RSpec co中執行此操作無需推出額外的寶石:

# in spec/support/controllers/mailer_helper.rb 
module Controllers 
    module MailerHelpers 
    def last_email 
     ActionMailer::Base.deliveries.last 
    end 

    def extract_confirmation_token(email) 
     email && email.body && email.body.match(/confirmation_token=(.+)">/x)[1] 
    end 
    end 
end 

# in spec_helper.rb in RSpec.configure block 
config.include Controllers::MailerHelpers, type: :controller 

# in spec/controllers/confirmations_controller_spec.rb 
describe ConfirmationsController do 
    it "confirms registered user" do 
     user.confirmation_token 
     token = extract_confirmation_token last_email 
     get :show, confirmation_token: token 

     # fill in your expectation here 
     expect(user...).to be[...] 
    end 
end 

希望這會有所幫助。

0

繼demisx的回答,我不能讓它不實際調用send_confirmation_instructions方法和重裝user對象工作:

require 'rails_helper' 

RSpec.describe Users::ConfirmationsController, type: :controller do 

    describe 'ConfirmationsController' do 
    before(:each) do 
     @user = FactoryGirl.build(:user) 
     @request.env["devise.mapping"] = Devise.mappings[:user] 
    end 

    it "confirms registered user" do 
     @user.send_confirmation_instructions 
     token = extract_confirmation_token last_email 
     get :show, confirmation_token: token 

     expect(@user.reload.confirmed?).to be(true) 
    end 
    end 

end