0

更新:

“RackTest 驱动程序无法单击不在表单中的按钮。” 通过 jnicklas

参考:https ://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/ECc4U_dux08

起源:

我已经为这个问题苦苦挣扎了几个小时,但不明白为什么。请你帮助我好吗?

index.html.haml

.welcome.ac
    %h1.brand.text-info GroupMeal
        %h2.tagline
        The simplest way of scheduling meals with your groups and friends
    %p
        - if !current_user
            %button#fblogin.btn.btn-info.btn-large Login with Facebook
        - else
            %a.btn.btn-info.btn-large{:href => '/signout'} Sign out\

authentications_spec.rb

describe "page" do
  before do
    visit root_path
  end
  it { should have_button('Login with Facebook') } # 1. This case is passed
  describe "with valid information" do
    before do
      click_button('Login with Facebook') # 2. But this line is broken
    end
    it { should have_link('Sign out', href: '/signout') } 
  end
end

案例 1:验证“使用 Facebook 登录”按钮是否存在 --> 通过。
案例 2:click_button --> 失败并收到以下错误。

Failure/Error: click_button('Login with Facebook')
NoMethodError:
    undefined method `node_name' for nil:NilClass

我不明白为什么该按钮存在但无法单击。

4

1 回答 1

0

每个describe块都会创建一个新范围,因此 RSpec 不再位于第二个应用程序的根页面上。您要么必须指示 RSPec 再次访问根目录,要么将第一个before块更改为before(:each)块,以便它在每次测试之前运行。

RSpec 以这种方式设置每个描述块,因为在编写测试时,每个测试都应该能够独立运行——测试的顺序不重要,因此让一个测试为另一个测试设置条件是个坏主意.

如果我是你,我会这样写,假设你在描述“页面”块中放置的所有测试都应该测试根页面上的功能(如果是这样,我会将“页面”更改为“根页面“所以很清楚正在测试什么):

describe 'root page' do
  before(:each) do
    visit '/'
  end

  # Calling subject will allow you to keep using "it" 
  # in case you want to test other aspects of the root 
  # page here. It's worth noting that click_button
  # will tell you if it can't find the button you're
  # asking for, so you don't really need the next two
  # lines as things currently stand.
  subject { page }

  it { should have_button('Login with Facebook') }

  describe 'signing in with valid information' do
    specify do
      click_button('Login with Facebook')
      page.should have_link('Sign out', href: '/signout' )
    end
  end
end
于 2013-03-24T02:35:53.950 回答