![](/img/trans.png)
[英]Rails: request.subdomains raises undef method for request when in application controller
[英]How can I stub or mock the request.subdomains method in Rails?
我正在嘗試在我的 Rails 應用程序中編寫一些功能測試,在 application_controller.rb 中我有這個:
before_filter :current_account
def current_account
@current_account ||= Account.find_by_subdomain!(request.subdomians.first)
end
運行測試時, request.subdomains
不包含我要查找的有效子域,因此無法運行任何功能測試。
是否可以存根current_account
方法或模擬request.subdomains
object?
在您的功能測試中,您應該能夠做到(使用 mocha):
@request.expects(:subdomains).returns(['www'])
對我(以及 Rails 2.3.4)來說,正確的說法是
@controller.request.expects(:subdomains).returns(['www'])
因為我無法直接訪問@request。
@controller.instance_variable_set(:@request, OpenStruct.new({:subdomains => 'www'}))
您可以訪問 ruby 中的任何內容:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.