How can I stub or make fun of the request.subdomains method in Rails?

I am trying to write some functional tests in my rails application, and in application_controller.rb I have this:

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

When performing tests, it request.subdomainsdoes not contain valid subdomains that I am looking for, and makes it impossible to run any functional tests.

Is it possible to mute a method current_accountor make fun of an object request.subdomains?

+3
source share
3 answers

In your functional test, you can do (using mocha):

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

For me (and with Rails 2.3.4) the correct instruction

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

@request.

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

- ruby:)

+1

Source: https://habr.com/ru/post/1710631/


All Articles