gpt4 book ai didi

ruby-on-rails-3 - rspec 忽略skip_before_filter?

转载 作者:行者123 更新时间:2023-12-04 07:09:47 24 4
gpt4 key购买 nike

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

在我们的库 Controller 中,我们跳过这个 before_filter .

skip_before_filter :authenticate_user!, :only => :show

当我们使用 capybara 运行一个简单的功能测试时和 rspec测试失败。
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 下,它工作正常。

Controller 代码:
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

最佳答案

我想您可以做的第一件事是查看“访问 '/en/library/computers'”是否实际上正在调用 Controller 中的 show 操作。如果您使用的是访问 show 操作的静态路径,请使用它代替您当前的 url 路径,使其看起来像:

visit library_path('computers')

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

更新:

看起来您的 Controller 可能不会将“计算机”解释为 id。在你的路由中,如果你将成员路由添加到你的库资源中会怎样:
resources :libraries do
member do
get :computers
end
end

在您的 Controller 中添加:
def computers
@categories = Category.all
end

并更改您 skip_before_filter 以使用 :computers

关于ruby-on-rails-3 - rspec 忽略skip_before_filter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9497041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com