0

My application controller looks like:

class ApplicationController
  before_action :set_customer

  def customer?
    @customer.nil?
  end

  private
    def set_customer
       if request.host != "example.com"
          @customer = Customer.find_by_domain("...")
       else
         if request.subdomain != "www"
           @customer = Customer.find_by_sub("...")
         end
       end
    end    


end

How can I make tests for the ApplicationController?

I want to pass in URLS and then check if @customer is nil etc.

e.g URLS to test

example.com
www.example.com
google.com
hello.example.com
cool breeze
  • 4,461
  • 5
  • 38
  • 67
  • may be this can help you http://stackoverflow.com/questions/4739116/how-to-test-applicationcontroller-method-defined-also-as-a-helper-method – sts Jan 12 '16 at 18:58

1 Answers1

0

You shouldn't test instance variables. Move the code to a service object if needed.

assigns is going to be deprecated in Rails 5, so will be probably removed at some point. In controller tests, you want to check that the response is what you expect (response code) based on your user (authentication) and authorization, and possibly that the correct format is used.

In a different test (a "view" test, I mean the integration tests) you check also that the output in the view contains some value based on your instance variable.

In this case you can simply test that the service calls Customer.find_by_sub (or by_domain) based on your clause, and that's all you need to test, instance variable shouldn't be something you care about.

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
  • I see what your saying, but I have logic that looks for request.host or request.subdomain that I have to test. I can't wrap that into another method because I sometimes use request.host and sometimes request.subdomain. – cool breeze Jan 12 '16 at 20:14
  • You can create a service object and pass the request in there and handle everything through there. It will also be easier to stub `host` and `subdomain` because you can pass around a mocked object – Francesco Belladonna Jan 12 '16 at 20:42