Question

I have a very simple app I'm trying to test with rspec. I'm having a hard time figuring out if my configuration is the problem, or if it's the code. The first line of /people/index.html.erb is <h1>Totally Awesome Page</h1>.

The error:

expected to find text "Totally Awesome Page" in "Index"
./spec/features/people_spec.rb:19:in `block (3 levels) in <class:PeopleSpec>'

test gems:

group :development, :test do
  gem 'rspec'
  gem 'rspec-rails'
  gem 'capybara'
  gem 'factory_girl_rails'
end

spec/features/people_spec.rb:

require 'spec_helper'

class PeopleSpec < ActionDispatch::IntegrationTest

  describe "People" do

    describe "Index" do

     before { visit '/people'}

      it { should have_content('Totally Awesome Page')}

    end
  end
end

spec/spec_helper:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'minitest/autorun'
require 'capybara/rails'
require 'capybara/rspec'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

  config.use_transactional_fixtures = true

  config.infer_base_class_for_anonymous_controllers = false

  config.order = "random"
  config.include Capybara::DSL
end

routes:

people_path     GET     /people(.:format)   people#index
              POST  /people(.:format)   people#create
new_person_path     GET     /people/new(.:format)   people#new
edit_person_pathGET     /people/:id/edit(.:format)  people#edit
person_path     GET     /people/:id(.:format)   people#show
              PATCH     /people/:id(.:format)   people#update
              PUT   /people/:id(.:format)   people#update
              DELETE    /people/:id(.:format)   people#destroy 
Was it helpful?

Solution

Add gem 'launchy' and then you can use save_and_open_page after visit to see how rendered page looks like.

Add subject { page } to the spec to fix it.

If you say it than it have to point to something. In your test it doesn't. You can use should on page to do that (page is not the only one you can test. There are more, like response or even lambdas) that way:

it "some description" do
  page.should have_content('foo')
end

or if you have many tests for page than you can write them in a short way:

it { should have_content('foo') }

but you need to specify subject for it first:

subject { page }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top