How to mock request object for rspec helper tests? How to mock request object for rspec helper tests? ruby-on-rails ruby-on-rails

How to mock request object for rspec helper tests?


You have to prepend the helper method with 'helper':

describe ApplicationHelper do  it "should prepend subdomain to host" do    helper.root_with_subdomain("test").should = "test.xxxx:xxxx"  endend

Additionally to test behavior for different request options, you can access the request object throught the controller:

describe ApplicationHelper do  it "should prepend subdomain to host" do    controller.request.host = 'www.domain.com'    helper.root_with_subdomain("test").should = "test.xxxx:xxxx"  endend


This isn't a complete answer to your question, but for the record, you can mock a request using ActionController::TestRequest.new(). Something like:

describe ApplicationHelper do  it "should prepend subdomain to host" do    test_domain = 'xxxx:xxxx'    controller.request = ActionController::TestRequest.new(:host => test_domain)    helper.root_with_subdomain("test").should = "test.#{test_domain}"  endend


I had a similar problem, i found this solution to work:

before(:each) do  helper.request.host = "yourhostandorport"end