2013-01-16 48 views
4

這裏是我的完整規格:水豚之前(:所有)在rspec的

require 'spec_helper' 

describe "Idea page", js: true do 

    subject { page } 

    before(:all) do 
    create(:idea) 
    visit root_path 
    click_link "Log In" 
    end 

    context "Single idea" do 
    before do 
     page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click 
    end 

    it { should have_selector('a',text:'Claim') } 
    it "should have a button for reporting the idea" 
    it "should have a button for opening all links" 

    describe "Claiming" do 
     before do 
     click_link "Claim" 
     end 
     it {should have_selector('a', text:'Claimed')} 
    end 

    end 
end 

沒有(:all)(即,當它只是before第一座),打開瀏覽器,點擊登錄鏈接,去到右側頁面,然後點擊鏈接。尼斯。

但是在它嘗試點擊第二個鏈接(「聲明」)之前,它會重新執行它,這既費時又容易出錯。所以我試圖用before(:all)來解決這個問題。

但現在,它只是彈出打開Firefox,等待片刻,並再次關閉它,而不做任何事情。測試失敗說:

Failures: 

    1) Idea page Single idea 
    Failure/Error: page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click 
    Capybara::ElementNotFound: 
     Unable to find xpath "//*[@id=\"accordion\"]/div/div[1]/a" 
    # ./spec/features/ideas_spec.rb:15:in `block (3 levels) in <top (required)>' 

    2) Idea page Single idea Claiming 
    Failure/Error: page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click 
    Capybara::ElementNotFound: 
     Unable to find xpath "//*[@id=\"accordion\"]/div/div[1]/a" 
    # ./spec/features/ideas_spec.rb:15:in `block (3 levels) in <top (required)>' 

很明顯,因爲瀏覽器頁面是空白的。

我錯過了什麼?謝謝。

編輯:也許有一些基本的東西我不明白。用before(:each)這裏是測試試圖做的事情:

1)登錄到web應用程序,確保有'Claim'按鈕。

2)登錄到web應用程序再次,打開手風琴再次,現在單擊「Claim`按鈕,看看有什麼事情發生。

所以每一步的開始都是一樣的,瀏覽器一次又一次地做同樣的事情。這是它應該如何?

如果是這樣,當我這樣做時爲什麼會出現錯誤?具體來說,before(:each)我得到這個:

Failures: 

    1) Idea page Single idea Claiming 
    Failure/Error: it {should have_selector('a', text:'Claimed')} 
    Selenium::WebDriver::Error::UnhandledAlertError: 
     Modal dialog present 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/command_processor.js:10287:in `nsCommandProcessor.execute' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/driver_component.js:7328:in `Dispatcher.executeAs/<' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/driver_component.js:7488:in `Resource.handle' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/driver_component.js:7435:in `Dispatcher.dispatch' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/driver_component.js:10119:in `WebDriverServer/<.handle' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1935:in `unknown' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:2261:in `ServerHandler.handleResponse' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1168:in `Connection.process' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1616:in `RequestReader._handleResponse' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1464:in `RequestReader._processBody' 
    # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1333:in `RequestReader.onInputStreamReady' 
    # ./spec/features/ideas_spec.rb:26:in `block (4 levels) in <top (required)>' 

即使我看到瀏覽器點擊按鈕,將其以「爆料」,並沒有顯示出有模態對話框。

編輯2:我站好了!那裏那裏的模態對話!我修正了JS停止顯示它,現在測試通過了。我仍然認爲框架必須從頭開始重複整個序列(看起來像浪費工作),但無論如何,這很奇怪。謝謝!

+2

如果您使用[shared_context](https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-context),則可以將登錄過程放在該處,並使其更清晰你在做什麼'include_context「登錄後''...它減少了打字! – iain

+0

@iain再次來救援! :) 謝謝!肯定會檢查出來。 – ezuk

回答

2

這是因爲測試數據,包括水豚會話數據(如登錄狀態)和由create(:idea)創建的模型在規格之間被刪除。

你想要before(:each)而不是:all,即使它更費時。

+2

一些澄清,當@ezuk剛纔'之前',這是做一個'之前(:每個)'。它們是等價的。 – MrDanA

+0

不,'之前(:每個)'已經是默認值,這正是我不想要的。我在提問之前閱讀過文檔。:) – ezuk

+2

如果你運行'before(:all)'並且做一個'create(:idea)',那麼該模型將在你的規範之間被破壞。水豚會破壞它的會話數據等。你非常不希望'before(:all)' –