title: RSpec-rails-capybara - different failures with :js => true and without
body: <p>I'm building a setup screen for billing of individuals. The controller/views are in the Admin namespace.</p>
  
  <p>When run the first test without :js => true I get one failure, which I assume is down to the fact that the link does not work as its a helper which uses a js script to build a nested set of fields (Based on Railscasts Single form, multiple tables - Nested Attributes).</p>
  
  <pre><code>Failures:
  
    1) Patient Setup create patient bill heading - with extended details -with valid data
       Failure/Error: fill_in "Extended Bill Heading", :with =&gt; 'Regular Registration'
       Capybara::ElementNotFound:
         cannot fill in, no text field, text area or password field with id, name, or label 'Extended Bill Heading' found
       # (eval):2:in `fill_in'
       # ./spec/requests/admin/patient_setup_pages_spec.rb:52:in `block (4 levels) in &lt;top (required)&gt;'
  
  Finished in 0.83047 seconds
  3 examples, 1 failure, 2 pending
  </code></pre>
  
  <p>But when I use :js = true, I'm getting a failure which appears to be from the login with Invalid user/password flashing up on the screen when running.</p>
  
  <p><img src="https://i.stack.imgur.com/pNQBQ.jpg" alt="enter image description here"></p>
  
  <pre><code>Failures:
  
    1) Patient Setup create patient bill heading - with extended details -with valid data
       Failure/Error: click_link 'System'
       Capybara::ElementNotFound:
         no link with title, id or text 'System' found
       # (eval):2:in `click_link'
       # ./spec/requests/admin/patient_setup_pages_spec.rb:22:in `block (2 levels) in &lt;top (required)&gt;'
  
  Finished in 6.33 seconds
  3 examples, 1 failure, 2 pending
  </code></pre>
  
  <p>Here is the code that backs all this up.</p>
  
  <pre><code>spec/requests/admin/patient_setup_spec.rb
  
  require 'spec_helper'
  
  feature 'Patient Setup' do
  
    let!(:ci_user) { FactoryGirl.create(:user, 
                                         name: "configuration engineer", 
                                         password: "password",
                                         password_confirmation: "password"
                                       ) }
    let!(:admin_role) { FactoryGirl.create(:admin_role) }
    let!(:assignment) { FactoryGirl.create(:assignment, 
                                           :role =&gt; admin_role, 
                                           :user =&gt; ci_user 
                                           ) } 
  
    before do 
      visit login_path
      fill_in "Name", with: "configuration engineer"
      fill_in "Password", with: "password"
      click_button "Login"
      save_and_open_page
      click_link 'System'
      click_link 'Patient Setup'
    end
  
    describe "create patient bill heading" do
      before do
        click_link 'New Bill Heading'
        fill_in 'Bill heading', :with =&gt; 'Consultation' 
        fill_in 'Abbreviation', :with =&gt; "CON"       
      end
  
      context "- no extended details" do
        pending
  
        scenario "- with valid data" do
          pending
          click_button 'Create Patient bill heading' 
          page.should have_content('Patient Bill Heading created.')
        end
      end
  
      context "- with extended details", :js =&gt; true do #(without :js =&gt; true 1st error)
        before do
          # save_and_open_page
          click_link "Extended Bill Heading"
          # save_and_open_page        
        end
  
        scenario "-with valid data" do
          save_and_open_page
          fill_in "Extended Bill Heading", :with =&gt; 'Regular Registration'
        end
      end
  
    end
  end
  </code></pre>
  
  <p>Here is my factory setup.</p>
  
  <pre><code>spec/factories.rb
  
  FactoryGirl.define do
  
    # Users, Roles
    factory :user do
      name     "Joesephine Bloggs"
      password "testmenow"
      password_confirmation "testmenow"
    end
  
    factory :admin, :class =&gt; User do
      sequence(:name) { |n| "Administrator-#{n}" }
      password "adminiam"
      password_confirmation "adminiam"
      after(:create) do |user|
        FactoryGirl.create(:assignment, :role =&gt; FactoryGirl.create(:admin_role), :user =&gt; user )
      end
    end
  
    factory :role do
      description { "Clerical-#{rand(99)}" }
  
      factory :admin_role do
        description "Admin"
      end
    end
  
    factory :assignment do
      user
      role
    end
  
    # Patients Module
  
    factory :patient_bill_heading do
  
  
        sequence(:bill_heading) { |n| "bill-heading-#{n}" }
        sequence(:abbreviation) { |n| "abbreviation-#{n}" }
  
  
        factory :delete_patient_bill_heading, :class =&gt; PatientBillHeading do
          bill_heading :bill_heading
          abbreviation :abbreviation
        end
  
    end  
  end
  </code></pre>
  
  <p>Here is the link in my view to call the helper that generates the nested attribute fields.</p>
  
  <pre><code>&lt;p&gt;
    &lt;%= link_to_add_fields "Extended Bill Heading", f, :patient_extended_bill_headings %&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>And here is the helper.</p>
  
  <pre><code>helpers/application_helper.rb
  
    def link_to_add_fields(name, f, association, options={})
      defaults = {
        :partial_name =&gt; nil
      }
      options = defaults.merge(options)
  
      new_object = f.object.send(association).klass.new
      id = new_object.object_id
      fields = f.fields_for(association, new_object, child_index: id) do |builder|
        if options[:partial_name].nil?
          render(association.to_s.singularize + "_fields", f: builder)
        else
          render(options[:partial_name], f: builder)
        end
      end
      link_to("#{name} &lt;i class='icon-plus icon-white'&gt;&lt;/i&gt;".html_safe, 
              '#', 
              class: "btn btn-success add_fields",
              data: {id: id, fields: fields.gsub("\n", "")}
             )
    end
  </code></pre>
  
  <p>I'm trying to improve my RSpec testing knowledge, as I have successfully built this working in my application after an hour trying to figure out why I was getting test failures. So in the app it works, but I want to understand how to make my test pass.</p>
  
  <p>Is my assertion that one error is due to using js to create the link, and capybara is not running it as I don't use the :js => true option?</p>
  
  <p>Can anybody see what I'm doing wrong when using the :js => true option?</p>