简体   繁体   中英

Capybara and Rspec. Capybara not Rendering page in Test, with visit or get method in rails

Am a beginner, learning Ruby on Rails and I have an issue getting my test to work. Am using rspec and capybara. The problem is Capybara is not rendering the page or its not getting to the right page. The page opens properly in the browser when I execute: $rails s. But when I test with save_and_open_page, the html is empty.

My Specs

The following is my first PagesController spec:

 require 'spec_helper'

describe PagesController do
describe "GET 'home'" do
    it "returns http success" do
      visit pages_home_path
      save_and_open_page #at this point it opens the a blank html page
    end
  end
end

My controller spec, this opens a blank page.

I tried an alternative syntax for the Pagescontroller spec:

require 'spec_helper'

describe PagesController do

  describe "GET 'home'" do
    it "returns http success" do
       get :home           
       response.should render_template('home')
       response.should have_content("home") #it fails here
     end
  end
end

This shows the following error in the console Failures:

1) PagesController GET 'home' returns http success Failure/Error: response.should have_content("home") expected there to be text "home" in "#" # ./spec/controllers/pages_controller_spec.rb:10:in `block (3 levels) in '

Finished in 1.91 seconds 1 example, 1 failure

Other Configurations:

In the spec_helper I have added the following line, the rest is untouched.

require 'capybara/rspec'

* Gem file: *

    gem 'rails', '3.1.0'
    group :development, :test do
              gem 'autotest-rails'
      gem 'sqlite3'
      gem 'ruby-debug19', :require => 'ruby-debug'
      gem 'cucumber-rails', :require => false
      gem 'cucumber-rails-training-wheels'
      gem 'database_cleaner'
      gem 'capybara'
      gem 'launchy'
      gem 'rspec-rails'
      gem 'simplecov'
      gem 'rspec-core', '2.8.0'
    end
    group :assets do
      gem 'therubyracer'              
      gem 'sass-rails', "  ~> 3.1.0"
      gem 'coffee-rails', "~> 3.1.0"
      gem 'uglifier'
    end

    gem 'jquery-rails'
    gem 'haml'

I have not installed Webrat and verified its not in my Gem list. Thank you for your help.

From the documentation it seems that you should tag your specs as feature or put them into the spec/features folder.

For me it seems like you have the default behavior for controller test, which is to not render anything. You could override this, but I think it'll be better to have your integration tests separate from the controller tests.

Sounds like your second test is making a request and can't find the content, because the content is not in "response", but in "response.body", try like this:

it "returns http success" do
   get :home           
   response.should render_template('home')
   response.body.should have_content("home") 
 end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM