我有一个请求规范,可以在一个 block 内进行多次访问调用(访问“/sessions/new”和访问“/admin”)。这导致:
ActionView::Template::Error:
undefined local variable or method `view_factory' for #<#<Class:0x007fedda1d5180>:0x007fedda1bb118>
有什么办法可以解决这个问题吗?谢谢。代码是:
describe "Admin" do
before do
visit new_session_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Submit"
end
describe "GET /admin" do
it "should be successful" do
visit admin_dashboard_path
end
end
end
更新:
经过一些搜索,我发现只有在使用 Spork 运行时才会出现错误。 .这是我配置 Spork 的 spec_helper.rb
文件:
require 'rubygems'
require 'spork'
require 'simplecov'
ENV["RAILS_ENV"] ||= 'test'
SimpleCov.start if ENV["COVERAGE"]
Spork.prefork do
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :mocha
config.include Auth::Helper
end
end
最佳答案
我遇到了类似的问题,并找到了一些解决方法。我假设 view_factory
是在插件中定义的辅助方法,它包含在应用程序辅助程序中,类似于 ActionController::Base.helper FactoryHelperModule
。
我所做的是将以下代码片段添加到我的 app/helpers/application_helper.rb 中:
if Rails.env.test?
include FactoryHelperModule
end
如果辅助方法在一个模块中并且如果辅助方法像我的那样被声明,那么这有可能会起作用。不过,我还没有找到为什么会这样。
顺便说一句,我在 rails 3.0.4
和 spork 0.9.0.rc9
关于ruby-on-rails - 使用 RSpec 和 Capybara 多次调用访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7764388/