嗨,我尝试开始我的第一个 RoR 项目,但一开始就陷入困境:(
我的 Gemfile 中有水豚 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的第一行添加了水豚:
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/controller/static_pages_controller_spec.rb 中的正确标题时:
it "have propper title" do
get :home
expect(page).to have_title "Testowy tytuł"
end
我收到错误:未定义的局部变量或方法页
我厌倦了自己解决这个问题,但每个人都只说添加需要“capybara/rspec”
控制器规格 https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs无法访问 Capybara 的助手:功能规格 https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec do.
您可以在以下位置创建一个测试文件/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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)