Search code examples
rspeccapybararspec-rails

Rspec/Capybara features tests not running, even with _spec.rb ending


When I run my Rspec feature examples, they are not being detected. When I specify the route to my /features folder, the message I get is " No examples found". I'm not sure if it's a requirement issue or something I'm missing with my test.

my feature test:

require "rails_helper"
require 'capybara/rspec'



feature "user creates student", :type => :feature do
background do
    user = create :user
scenario "with valid data" do
    visit '/students/new'
    within("form") do
        fill_in ":first_name", :with => 'jason'
        fill_in ":last_name", :with => 'foobar'
        fill_in ":user_id", :with => '1'
    end
    click_button 'Submit'
    expect(page).to have_content 'jason foobar'

  end
end


  feature "user cannot create student" do
   background do
    user = create :user
    scenario "with invalid data" do
    visit '/students/new'
    within("form") do
        fill_in ":first_name", :with => :nil
        fill_in ":last_name", :with => 'foobar'
        fill_in ":user_id", :with => '1'
    end
    click_button 'Submit'
    expect(studnt.errors[:first_name]).to include("can't be blank")

  end
end
 end


 end

Solution

  • Your spec is getting run, but because it is malformed, there are no examples in it.

    If you were to format it properly (e.g. in terms of indentation), it would be more clear, but your background blocks include both of the associated scenarios.

    You need to delete the last two end statements in your file and insert end statements at the end of each background, as follows:

    require "rails_helper"
    require 'capybara/rspec'
    
    feature "user creates student", :type => :feature do
      background do
        user = create :user
      end
    
      scenario "with valid data" do
        visit '/students/new'
        within("form") do
          fill_in ":first_name", :with => 'jason'
          fill_in ":last_name", :with => 'foobar'
          fill_in ":user_id", :with => '1'
        end
        click_button 'Submit'
        expect(page).to have_content 'jason foobar'
      end
    end
    
    feature "user cannot create student" do
      background do
        user = create :user
      end
    
      scenario "with invalid data" do
        visit '/students/new'
        within("form") do
          fill_in ":first_name", :with => :nil
          fill_in ":last_name", :with => 'foobar'
          fill_in ":user_id", :with => '1'
        end
        click_button 'Submit'
        expect(studnt.errors[:first_name]).to include("can't be blank")
      end
    end