[英]How can I stub or mock the request.subdomains method in Rails?
I am trying to write some functional tests in my rails app, and in the application_controller.rb I have this:我正在尝试在我的 Rails 应用程序中编写一些功能测试,在 application_controller.rb 中我有这个:
before_filter :current_account
def current_account
@current_account ||= Account.find_by_subdomain!(request.subdomians.first)
end
When running tests, request.subdomains
doesn't contain the valid subdomains I'm looking for and makes it impossible to run any functional tests.运行测试时,
request.subdomains
不包含我要查找的有效子域,因此无法运行任何功能测试。
Is it possible to stub the current_account
method or mock the request.subdomains
object?是否可以存根
current_account
方法或模拟request.subdomains
object?
In your functional test you should be able to do (using mocha):在您的功能测试中,您应该能够做到(使用 mocha):
@request.expects(:subdomains).returns(['www'])
To me (and with Rails 2.3.4), the correct statement is对我(以及 Rails 2.3.4)来说,正确的说法是
@controller.request.expects(:subdomains).returns(['www'])
since I cannot access to @request directly.因为我无法直接访问@request。
@controller.instance_variable_set(:@request, OpenStruct.new({:subdomains => 'www'}))
you can access anything in ruby:)您可以访问 ruby 中的任何内容:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.