Unable to setup Rspec & Capybara in Rails 3.2

人盡茶涼 提交于 2019-12-01 03:07:56

问题


I am creating a sample application for rspec testing, and I followed the below steps In Gemfile:

gem "rspec-rails", :group => [:test, :development]
group :test do
  gem "factory_girl_rails"
  gem "capybara"
  gem "guard-rspec"
end

Then executed the below steps:

  1. bundle.
  2. rails g rspec:install.
  3. mkdir spec/support spec/models spec/routing.

Added "require capybara/rspec" to my spec_helper file

Created a sample test as below:

require 'spec_helper'
describe "Users" do
  describe " List users" do
    it "List all users" do
      get users_path
      page.has_content?('List Users')
    end
  end
end

but it doesn't work, getting the below error


undefined local variable or method `page' 

* I suspect that I was not configured the capybara properly, Let me know the proper way to configure the capybara.


回答1:


Since you didn't specify a version for Capybara in your Gemfile, I assume you've got version >= 2.0, which means that any tests that use page should go under a spec/features directory.

Have a look at the following links for more information:

  • rspec-rails and capybara 2.0: what you need to know
  • rspec-rails gem Capybara page

If you don't want to use a spec/features directory, you should be able to mark a test as a feature in the following way:

require 'spec_helper'
describe "Users" do
  describe " List users", type: :feature do
    it "List all users" do
      get users_path
      page.has_content?('List Users')
    end
  end
end



回答2:


Got working after including the capybara dsl to spec helper

config.include Capybara::DSL



回答3:


Its worth checking out the new Capybara DSL for integration tests (note the features directory):

# spec/features/user_list_spec.rb
require 'spec_helper'

feature 'User list' do
  scenario 'List all users' do
    visit users_path
    expect(page).to have_content 'List Users'
  end
end

I wrote a blog post with some detailed information on End-to-end testing with RSpec integration tests and Capybara using RSpec 2.0 expect syntax, along with Capybara DSL for feature/scenarios.




回答4:


You could also just add this to your spec_helper.rb:

require 'capybara/rspec'


来源:https://stackoverflow.com/questions/14028247/unable-to-setup-rspec-capybara-in-rails-3-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!