簡體   English   中英

如何存根或模擬 Rails 中的 request.subdomains 方法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM