多次致电 RSpec 和水豚参观

发布于 2024-12-09 22:56:33 字数 1215 浏览 1 评论 0原文

我有一个请求规范,可以在单个块内进行多次调用访问(访问“/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

带上头具痛哭 2024-12-16 22:56:33

我遇到了类似的问题并找到了一些解决方法。我假设 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

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

伴我老 2024-12-16 22:56:33

不知道这是否会解决您的问题,但是 before 需要位于传递给 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文