2013-01-16 45 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)' –