2013-02-14 42 views
0

我想在需要身份驗證(通過設計)的Web應用程序上測試一個操作。具體的操作使用JavaScript,因此我申請了js選項來規範這樣:請求規格爲JavaScript失敗,設計身份驗證

scenario "User wants to fax a single document", js: true do 
    reset_email 
    @doc = @user.documents.create(FactoryGirl.attributes_for(:document)) 
    visit "/documents/#{@user.id}" 

    click_on "send_document_#{@doc.id}" 
    last_email.should eq(@doc) 
end 

控制器在電子郵件十歲上下的方式發送傳真。我不知道爲什麼;我沒有寫。無論如何,在(使用水豚使用RSpec)此功能規格的頂部,我驗證文件中使用

before(:each) do 
    # Signs in as an admin 
    @company = FactoryGirl.create(:company) 
    @subscription = FactoryGirl.create(:admin_subscription, company_id: @company.id) 
    @user = FactoryGirl.create(:user, company_id: @company.id) 
    login_as @user, scope: :user 
end 

所有其他規格(這還需要登錄)依然通過,有我認爲它必須做與JavaScript。因此,js選項在Firefox中打開瀏覽器,並且該頁面不是正確的內容。它說

500 Internal Server Error 
    undefined method `status' for nil:NilClass 

我在網上搜索,發現只有迴應說,不要在我的叫responseaction控制器動作。放心,我沒有這樣的行爲;只有RESTful行動以及兩個加法:

def send_document 
    @to = params[:to].gsub(/([() \-]+)/, '') 
    #The below parses the number to format it for InterFax 
    #It adds a "+1" to the front, and a dash in the middle 
    #of the number where needed. 
    @to = "+1"[email protected][0..-5]+"-"[email protected][-4,4] 
    @document = Document.find(params[:id]) 
    DocumentMailer.send_document(@to, @document).deliver 
    render :template => false, :text => 'true' 
end 
def email_document 
    @to = params[:to] 
    @document = Document.find(params[:id]) 
    DocumentMailer.email_document(@to, @document).deliver 
    render :template => false, :text => 'true' 
end 

任何人都可以幫助理解這些錯誤嗎?很多此應用程序使用javascript,並且我確實需要一種方式來在登錄時測試這些操作。

回答

0

在沒有首先測試操作已完成的情況下,要小心檢查非UI條件。當你這樣做:

click_on "send_document_#{@doc.id}" 
    last_email.should eq(@doc) 

記住,JavaScript是一種瀏覽器實例,這是不是在同一進程中測試代碼中運行。它可以幫助移動之前,檢查網頁上的更新:

click_on "send_document_#{@doc.id}" 
    page.should have_content("Email sent") # for example 
    last_email.should eq(@doc) 

水豚聲稱自己是非常聰明的等待頁面元素是可見的 - 情況因人而異。

如果您的應用需要登錄,您的請求規範應使用正常的登錄過程 - 訪問登錄頁面,輸入憑據,然後導航到要測試的頁面。