【问题标题】:rspec ignores skip_before_filter?rspec忽略skip_before_filter?
【发布时间】:2023-03-09 13:12:01
【问题描述】:

我们遇到了一个奇怪的问题。在应用程序控制器中,我们有一个before_filter 集,它需要使用devise 进行身份验证,并在需要时重定向到登录页面。

在我们的库控制器中,我们跳过了这个before_filter

skip_before_filter :authenticate_user!, :only => :show

当我们使用 capybararspec 运行简单的功能测试时,测试失败。

it "should get only english articles within the category 'computers'" do
    visit '/en/library/computers'
    page.should have_content('computers')
end

看起来它没有跳过这个过滤器。页面内容为登录页面。

当我们使用rails server 运行它时,它运行良好。

任何想法为什么它会以这种方式运行或寻找什么来解决这个问题?

更新:

值得补充的是,这只发生在 Linux 上。在具有“相同”设置的 MacOS 10.7 下,它可以正常工作。

控制器代码:

class Library::CategoriesController < ApplicationController
  skip_before_filter :authenticate_user!, :only => [:show]

  # GET /categories/1
  # GET /categories/1.json
  def show

    @categories = Category.all
    @category = Category.find(params[:id])

    @articles = @category.articles.where(:locale => I18n.locale)
    respond_to do |format|
      format.html # show.html.erb
    end
  end
end

application_controller 看起来像(没有 set_i18n_locale_from_url):

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :set_i18n_locale_from_url, :authenticate_user!
end

路线:

namespace :library do
  get '/' => 'library#index', :as => 'library'
  resources :categories, :path => '', :only => [:show]
  resources :categories, :path => '', :only => []  do
    resources :articles, :path => '', :only => [:show]
  end
end

【问题讨论】:

  • 我看到了完全相同的行为。

标签: ruby-on-rails-3 rspec devise capybara


【解决方案1】:

我想你可以做的第一件事是看看“访问'/en/library/computers'”是否真的在你的控制器中调用显示操作。如果您使用的是访问 show 操作的 restful 路径,请使用它代替您当前的 url 路径,使其看起来像:

visit library_path('computers')

接下来在您正在测试的 show 方法中抛出一些调试器文本(输入“blah”),并确保它出现。也许通过调用严格路径会绕过 before_filters 做一些奇怪的事情。

更新:

看起来您的控制器可能不会将“计算机”解释为 id。在您的路由中,如果您将成员路由添加到您的库资源会怎样:

resources :libraries do
  member do
    get :computers
  end
end

然后在您的控制器中添加:

def computers
  @categories = Category.all
end

然后将您的 skip_before_filter 更改为使用 :computers

【讨论】:

  • 好的,我们试过了。结果相同。测试失败。 show 方法中的调试器输出不会被执行。
  • 在原帖上添加了一个想法
  • 试过了。没用。它甚至没有接近执行动作,导致 before_filter 之前启动。我用路由设置更新了最初的问题。也许这与它有关?同样有趣的是为什么这只发生在 linux 而不是 mac os x 下。
  • 它在运行rails server 时也能按预期工作,但只是在测试中中断,对吧?
  • 没错。使用rails server 时找不到模板。但在测试中甚至没有到达那里。在登录页面之前被重定向。
【解决方案2】:

我认为这与工厂和我们对类别模型的不同本地化有关。可能在 linux 下它使用不同的默认语言环境,这就是它失败的原因。

我删除了用于身份验证的 before_filter 并且大多数测试都通过了,但是会发生这种情况:

 1) Categories when user requests the category page 
      GET /en/library/computers 
        should get only english articles within the category computers
    Failure/Error: page.should have_content(@english_articles[0].title)
      expected there to be content "article 1 en" in "Bibliothek\nAdmin\nSign up\nLogin\n\n\n\n\n\n\n\n\nHome\n>\n\n\nLibrary\n>\n\n\Computer\n>\n\nThe spine\n\n\n\n\n\n\n\n\n\nComputer\n\n\n\n\n\n\n\n\n\n\n\n\nComputer\n\n\n\narticle 6 de\n\ncontent_6\n\n\narticle 7 de\n\ncontent_7\n\n\narticle 8 de\n\ncontent_8\n\n\narticle 9 de\n\ncontent_9\n\n\narticle 10 de\n\ncontent_10"
    # ./spec/requests/categories_spec.rb:20:in `block (4 levels) in <top (required)>'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多