Multiple Calls to Visit with RSpec and Capybara
I have a requests spec that makes multiple calls to visit within a single block (visits '/sessions/new' and visits '/admin'). This results in:
ActionView::Template::Error:
undefined local variable or method `view_factory' for #<#<Class:0x007fedda1d5180>:0x007fedda1bb118>
Any way to fix this? Thanks. The code is:
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
Update:
After some searching, I found that the errors only occure when running with Spork. Here is my spec_helper.rb
flile that configures Spork:
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开发者_C百科 f}
RSpec.configure do |config|
config.mock_with :mocha
config.include Auth::Helper
end
end
I had a similar problem and found a little workaround. I assume that view_factory
is a helper method defined in a plugin and it is included to the application helper with something like ActionController::Base.helper FactoryHelperModule
.
What I did was to include the following snippet to my app/helpers/application_helper.rb:
if Rails.env.test?
include FactoryHelperModule
end
If the helper methods are in a module and if the helper methods are declared like mine, there is a chance that this will work. I haven't found yet why this happens though.
Btw, I am on rails 3.0.4
and spork 0.9.0.rc9
Don't know if this will fix your issue, but before
needs to be inside the block passed to describe
:
describe "GET /admin" do
before do
visit new_session_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Submit"
end
it "should be successful" do
visit admin_dashboard_path
end
end
See my solution here: http://railsgotchas.wordpress.com/2012/01/31/activeadmin-spork-and-the-infamous-undefined-local-variable-or-method-view_factory/
精彩评论