ruby-on-rails - 如何在 Rails 测试中伪造子域查找?

标签 ruby-on-rails testing functional-testing

我定义了以下过滤器:

# 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。删除设置 block 会导致我的所有测试失败,因为 find_account 找不到帐户,因此会引发 RecordNotFound 错误。

我做错了什么?

最佳答案

试试这个:

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

关于ruby-on-rails - 如何在 Rails 测试中伪造子域查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1813038/

相关文章:

c++ - 使用 cmake : how could I write a test to verify that a class is abstract?

java - Android Espresso,如何继续执行 onLongClick 和 slideDown?

Symfony2 功能测试 InvalidArgumentException : The current node list is empty

testing - 如何将变量值从一个 AutoIt script 脚本传递到下一个

ruby-on-rails - 在 View 规范中测试 "dynamic"页面标题 (Rails 4 + RSpec 3)

ruby-on-rails - 导出 Mixpanel 事件提要数据

html - 最多显示 3 行文本字段

ruby-on-rails - `require' : no such file to load -- mkmf (LoadError)

testing - 将测试用例从 Excel 上传到 Microsoft Test Manager 2013

azure - 属性错误: module 'pytest' has no attribute 'config'