gpt4 book ai didi

ruby-on-rails - Rspec 和 Capybara 未定义的局部变量或方法 `page'

转载 作者:行者123 更新时间:2023-12-04 19:04:05 26 4
gpt4 key购买 nike

嗨,我试图开始我的第一个 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/controller/static_pages_controller_spec.rb 中测试正确的标题时:
it "have propper title" do
get :home
expect(page).to have_title "Testowy tytuł"
end

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

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

最佳答案

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

关于ruby-on-rails - Rspec 和 Capybara 未定义的局部变量或方法 `page',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29448012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com