Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include routing in ApplicationHelper Spec when running trough Spork?

I have an rspec spec:

require "spec_helper"

describe ApplicationHelper do
  describe "#link_to_cart" do
    it 'should be a link to the cart' do
      helper.link_to_cart.should match /.*href="\/cart".*/
    end
  end
end

And the ApplicationHelper:

module ApplicationHelper
  def link_to_cart
    link_to "Cart", cart_path
  end
end

This works when visiting the site, but the specs fail with a RuntimeError about Routing not being available:

RuntimeError:
   In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers

So, I included the Rails.application.routes.url in my spec, the spec_helper-file and even the ApplicationHelper itself, to no avail.

Edit: I am running the tests via spork, maybe that has to do with it and is causing the issue.

How must I include these route helpers when running with Spork?

like image 629
berkes Avatar asked Apr 16 '13 14:04

berkes


1 Answers

You need to add include at module level of ApplicationHelper, because ApplicationHelper doesn't include the url helper by default. Code like this

module AppplicationHelper
  include Rails.application.routes.url_helpers

  # ...
  def link_to_cart
    link_to "Cart", cart_path
  end

 end

Then the code will work and your test will pass.

like image 189
Billy Chan Avatar answered Oct 23 '22 20:10

Billy Chan