John Cowan
John Cowan

Reputation: 1674

rspec Test Result Capybara::ElementNotFound

I've been asked to help out on a Rails app. I'm going through some failing rpsec tests and getting them to pass. I've got one on which I'm banging my head against a wall. We are using Rails 3.2.13 and Ruby 2.0 Note: I'm totally a nube on rspec.

I have a form with an id of vet-as-faculty. The test is looking for this and not finding it. I get the error:

Capybara::ElementNotFound:
   Unable to find css "form#vet-as-faculty

I get a similar error on other form elements in the test also, but the test is failing on or at this point.

I know this "usually" means the markup on the form is not correct or something in the test is misspelled (I've found a few of those). But this one I just cannot get. I've Googled on this error and found a bit of stuff, but nothing that seems to help me on this one. I'm hoping you gurus out there will see something simple for me.

Form/view (in part):

 <%= form_for @profile, :url => { :action => :vet_as_faculty }, :html => { :id => "vet-as-faculty" } do |f| %>
  <fieldset class="row1"><legend>Vet Me As Faculty</legend>
    <div class="form-pair" id="faculty-url">
    <div class="form-item">
      <label>Faculty URL</label>
    </div>
  <fieldset class="row2">
    <div class="form-submit" id="save-faculty">
      <%= f.submit "Send for Review" %>
    </div>
  </fieldset>
<% end %>

View source shows this as:

<form accept-charset="UTF-8" action="/research/chr/casestudies/profile/vet_as_faculty" class="edit_user" id="vet-as-faculty" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="FZtjli8HaGD3koUvSFAYR2Gy+waL8KDWflMNUo4tXXw=" /></div>
  <fieldset class="row1"><legend>Vet Me As Faculty</legend>
    <div class="form-pair" id="faculty-url">
      <div class="form-item">
        <label>Faculty URL</label>
      </div>
      <div class="form-value">
        <input id="user_faculty_url" name="user[faculty_url]" size="30" type="text" />
      </div>
  </fieldset>

  <fieldset class="row2">
    <div class="form-submit" id="save-faculty">
      <input name="commit" type="submit" value="Send for Review" />
    </div>
  </fieldset>
...

So, I see the for id as 'id="vet-as-faculty"'. but, the test does not see this.

The Test Code:

context "within the faculty vetting block" do
    it "has the necessary form fields" do
      within "form#vet-as-faculty" do 
        expect(page).to have_css("input#user_faculty_url")
        expect(page).to have_css("input#save-faculty")
      end
    end
 end

The error output for this one test:

3) Edit Profile View within the faculty vetting block has the necessary form fields
   Failure/Error: within "form#vet-as-faculty" do
   Capybara::ElementNotFound:
     Unable to find css "form#vet-as-faculty"
   # ./spec/features/edit_profile_view_spec.rb:48:in `block (3 levels) in <top (required)>'

I've tried to add more drill down type css in the test

within "#page #content form#vet-as-faculty" do

The form is inside div#content and content is inside div#page. This does not help either.

Any thoughts? Thanks for reading this.

Upvotes: 0

Views: 701

Answers (1)

DVG
DVG

Reputation: 17480

Best guess is that you haven't actually navigated to the page where your form is

before { visit new_my_model_path }

As a more general piece of advice. Avoid writing tests that just assert markup/css. Focus on the behavior you need this form to accomplish instead and your tests will be a lot more valuable.

Upvotes: 1

Related Questions