【问题标题】:How to fake out a subdomain lookup in Rails tests?如何在 Rails 测试中伪造子域查找?
【发布时间】:2009-11-28 16:58:08
【问题描述】:

我定义了以下过滤器:

# application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :find_account

  private

    def find_account
      @current_account = Account.find_by_subdomain!(request.subdomains.first)
    end
end

在我的测试中:

# users_controller_test.rb
class UsersControllerTest < ActionController::TestCase
  setup do
    @request.host = "test.myapp.local"
  end
  # ...
end

现在test 被定义为我在所有使用factory_girl 的请求之前加载的虚拟帐户的子域。但是,这是抛出一个 nil 对象错误,说 @request 是 nil。删除 setup 块会导致我的所有测试都失败,因为 find_account 找不到帐户,因此会引发 RecordNotFound 错误。

我做错了什么?

【问题讨论】:

  • 我也遇到了同样的问题,你找到解决办法了吗?
  • 有用的是发现你正在使用 request.subdomains 来获取子域,我曾经从 params[:subdomain] 获取它但是如果我设置 @request.host 或 @request.env 它不起作用['HTTP_HOST']

标签: ruby-on-rails testing functional-testing


【解决方案1】:

试试这个:

@request.env['HTTP_HOST'] = 'test.myapp.local'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-26
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    相关资源
    最近更新 更多