简体   繁体   中英

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:

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.

Is it possible to stub the current_account method or mock the request.subdomains object?

In your functional test you should be able to do (using mocha):

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

To me (and with Rails 2.3.4), the correct statement is

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

since I cannot access to @request directly.

@controller.instance_variable_set(:@request, OpenStruct.new({:subdomains => 'www'}))

you can access anything in ruby:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM