ruby-on-rails - 如何 stub 或模拟 Rails 中的 request.subdomains 方法?

标签 ruby-on-rails mocking integration-testing stubbing

我正在尝试在我的 Rails 应用程序中编写一些功能测试,在 application_controller.rb 中我有这个:

before_filter :current_account
def current_account
  @current_account ||= Account.find_by_subdomain!(request.subdomians.first)
end

运行测试时,request.subdomains 不包含我正在寻找的有效子域,因此无法运行任何功能测试。

是否可以 stub current_account 方法或模拟 request.subdomains 对象?

最佳答案

在您的功能测试中,您应该能够(使用 mocha):

@request.expects(:subdomains).returns(['www'])

关于ruby-on-rails - 如何 stub 或模拟 Rails 中的 request.subdomains 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1004832/

相关文章:

mysql - MySQL 主数据存储和 MongoDB 辅助数据存储之间的 ETL

ruby-on-rails - AngularJS错误: [$injector:nomod] when trying to load 'templates'

ruby-on-rails - rails 3 : How to prevent image caching when the image is specified in the CSS file?

python - 我可以在 python 单元测试中伪造/模拟我的模拟对象的类型吗

mocking - PHPUnit 如何获取模拟方法的真实返回值?

java - 使用可注入(inject)单例

Spring MockRestServiceServer 处理对同一 URI 的多个请求(自动发现)

ruby-on-rails - Begin 和 Rescue block 异常处理

java - 使用 Spring Security 进行集成测试

java - Jenkins 在多个操作系统上进行集成测试