加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 未定义的局部变量或方法capybara rspec

发布时间:2020-12-17 03:35:44 所属栏目:百科 来源:网络整理
导读:当我在前块之外使用capybara方法时,我一直收到这个奇怪的错误: $rspec . -e "PasswordResets"/spec/requests/password_resets_spec.rb:9:in `block (2 levels) in top (required)': undefined local variable or method `root_path' for #Class:0x00000003b
当我在前块之外使用capybara方法时,我一直收到这个奇怪的错误:

$rspec . -e "PasswordResets"
/spec/requests/password_resets_spec.rb:9:in `block (2 levels) in <top (required)>': undefined local variable or method `root_path' for #<Class:0x00000003bfa100> (NameError)

root_path确实存在.我有多个其他测试可行.但是看起来我只能在方法在前一个区块内时使用Capybara.这有效:

require 'spec_helper'
describe "PasswordResets" do
  subject { page }
  describe "it emails user when requesting a password reset" do
    before do
      visit root_path
    end
  end
end

这会产生错误:

require 'spec_helper'
describe "PasswordResets" do
  subject { page }
  describe "it emails user when requesting a password reset" do
    visit root_path
  end
end

这有效:

require 'spec_helper'
describe "PasswordResets" do
  it "emails user when requesting a password reset" do
    visit root_path
  end
end

我想要做的是使用主题{page}但是能够使用capybara而不必使用before do end块.知道我做错了什么吗?

我的spec_helper.rb文件:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment",__FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
end

我使用的是Capybara 1.1.2,Rspec 2.8.1的Rails 3.2.

解决方法

您不能将测试代码直接放在描述中.范围不同.把它放在之前或之后.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读