Rspec和Capybara未定义的局部变量或方法`page' [英] Rspec and Capybara undefined local variable or method `page'

查看:45
本文介绍了Rspec和Capybara未定义的局部变量或方法`page'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开始我的第一个RoR项目,并陷入了一个开始:(

Hi I tried to start my first RoR project, and get stuck, a the begining :(

我的Gemfile中有水豚宝石:

I have capybara gem in my Gemfile :

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的第一行中添加了水豚:

I added capybara on my first line in spec_helper:

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中:

and added it to 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 / controller / static_pages_controller_spec.rb中测试正确的标题时:

But when I tried to test the right title in 'spec/controller/static_pages_controller_spec.rb :

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

我遇到了一个错误:未定义的局部变量或方法页面

I got an error: undefined local variable or method page

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

I tired to solve this by my self but everyone only says about adding to require "capybara/rspec"

推荐答案

控制器规范无法访问Capybara的助手:功能规范

Controller specs don't have access to Capybara's helpers: feature specs do.

您可以在 / spec中创建测试文件/features/static_pages_spec.rb 与以下类似,以利用水豚:

You can create a test file at /spec/features/static_pages_spec.rb similar to the following to make use of 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

这篇关于Rspec和Capybara未定义的局部变量或方法`page'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆