我一直當我使用水豚方法的外塊之前得到這個奇怪的錯誤:未定義的局部變量或方法豚rspec的
$ 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
塊內時才能使用水豚。這工作:
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
我on Rails的3.2與水豚1.1.2,2.8.1 Rspec的。
你不需要使用'before(:all)'或'before(:each)'嗎?我並不認爲'以前做'本身是有效的。 – jefflunt 2012-03-13 18:41:51