Andreas Lyngstad
Andreas Lyngstad

Reputation: 4927

Request in application controller returns nil when testing with rspec

I use

request.subdomain 

in my application controller. In develepment mode it works, but when I test it it returns

undefined method `subdomain' for nil:NilClass

I have this test.

describe ApplicationController do
controller do
  def index
    render text: "hello"
  end   
    end
let(:firm){FactoryGirl.create(:firm, subdomain: "test")}
let(:user){FactoryGirl.create(:user, firm: firm)}
describe "current" do
  before(:each) do 
    @request.host = "#{firm.subdomain}.example.com"
      sign_in(user) 
      get :index, subdomain: "test"
  end
  it "should current_firm" do
     ApplicationController.new.current_firm.subdomain.should == "test"
  end
    end
end

This is the method in the application controller

def current_firm
   @current_firm ||= Firm.find_by_subdomain!(request.subdomain)
end

Why is the request method nil during this test?

Upvotes: 3

Views: 1548

Answers (1)

usha
usha

Reputation: 29369

it "should current_firm" do
   @request.host = "#{firm.subdomain}.example.com"
   sign_in(user) 
   get :index, subdomain: "test"
   assigns(:current_firm).subdomain.should == "test"
end

Upvotes: 2

Related Questions