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

記述ブロックは新しいスコープを作成するため、RSpec は 2 つ目のアプリのルート ページにはありません。RSPec に再度ルートにアクセスするように指示するか、最初のbeforeブロックをbefore(:each)ブロックに変更して各テストの前に実行するようにする必要があります。

RSpec は各記述ブロックをこのように設定します。なぜなら、テストを書くとき、各テストはそれ自身で実行できるべきだからです。テストの順序は重要ではありません。 .

もし私があなたなら、これを次のように書きます。describe "page"ブロックに配置するすべてのテストがルート ページの機能をテストすることになっていると仮定します (そうであれば、"page" を "root page" に変更します)。 "なので、何がテストされているかは明らかです):

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 に答える