Rspec和Capybara未定义局部变量或方法`page'

时间:2015-04-04 15:14:29

标签: ruby-on-rails rspec capybara

嗨,我试图开始我的第一个RoR项目,然后陷入困境,开始了:(

我的Gemfile中有capybara gem:

group :development, :test do
  gem 'byebug'
  gem 'web-console'
  gem 'spring'
  gem 'rspec-rails'
  gem 'capybara'  
  gem 'factory_girl_rails'
  gem 'database_cleaner'   
end

我在spec_helper的第一行添加了capybara:

require "capybara/rspec"

RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
    # This option will default to `true` in RSpec 4. It makes the `description`
    # and `failure_message` of custom matchers include text for helper methods
    # defined using `chain`, e.g.:
    #     be_bigger_than(2).and_smaller_than(4).description
    #     # => "be bigger than 2 and smaller than 4"
    # ...rather than:
    #     # => "be bigger than 2"
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

并将其添加到rails_helper:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'


# Add additional requires below this line. Rails is not loaded until this point!
require 'capybara/rspec'
require 'capybara/rails'

但是当我尝试在&spec; / static /page_controller_spec.rb中测试正确的标题时:

it "have propper title" do
    get :home
    expect(page).to have_title "Testowy tytuł" 
end

我收到错误:未定义的局部变量或方法页

我厌倦了自己解决这个问题,但是每个人都只是说要添加要求" capybara / rspec"

1 个答案:

答案 0 :(得分:4)

Controller specs无法访问Capybara的助手:feature specs

您可以在/spec/features/static_pages_spec.rb创建一个类似于以下内容的测试文件,以便使用Capybara:

require "rails_helper"

RSpec.feature "Static pages", :type => :feature do
  scenario "Visiting the home page" do
    visit "/"
    expect(page).to have_title "Testowy tytuł" 
  end
end