当我在前一个块之外使用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
确实存在。我有多个其他测试可行。但是当方法在before
块内时,我似乎只能使用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
我想要做的是使用subject { page }
但是能够使用水豚而不必使用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。
答案 0 :(得分:8)
您不能将测试代码直接放在describe
中。范围不同。将其放在before
或it
块中。