多次致电 RSpec 和水豚参观
我有一个请求规范,可以在单个块内进行多次调用访问(访问“/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 的 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
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 f}
RSpec.configure do |config|
config.mock_with :mocha
config.include Auth::Helper
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了类似的问题并找到了一些解决方法。我假设 view_factory 是在插件中定义的帮助器方法,并且它包含在应用程序帮助器中,其中包含诸如 ActionController::Base.helper FactoryHelperModule 之类的内容。
我所做的是将以下代码片段包含到我的 app/helpers/application_helper.rb 中:
如果辅助方法位于模块中并且如果辅助方法的声明与我的类似,这有可能会起作用。我还没有找到为什么会发生这种情况。
顺便说一句,我在 Rails
3.0.4
和 spork0.9.0.rc9
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 likeActionController::Base.helper FactoryHelperModule
.What I did was to include the following snippet to my app/helpers/application_helper.rb:
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 spork0.9.0.rc9
不知道这是否会解决您的问题,但是
before
需要位于传递给describe
的块内:Don't know if this will fix your issue, but
before
needs to be inside the block passed todescribe
:在这里查看我的解决方案: http://railsgotchas.wordpress.com/2012/01/31/activeadmin-spork-and-the-inknown-undefined-local-variable-or-method-view_factory/
See my solution here: http://railsgotchas.wordpress.com/2012/01/31/activeadmin-spork-and-the-infamous-undefined-local-variable-or-method-view_factory/