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

3

7

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?

Rochette answered 16/4, 2013 at 14:8 Comment(0)
C
9

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.

Circumvent answered 17/4, 2013 at 7:50 Comment(6)
I already tried that, and it does not make the test pass: the failure message stays the same.Rochette
@berkes, I've already verified that in my console before posting the answer. It works. In your question, I only saw you mentioned including it in test, but not ApplicationHelper module. That is incorrect.Circumvent
It's also NOT ApplicationController, but ApplicationHelperCircumvent
My wrong; a mistake in the question, but it changes little about my problem. I suspect spork being the problem here and will try moving that out and re-testing.Rochette
@berkes, I don't use Spork but plain Rspec. I just wrote test for helper for a static page in same code. And it passed. No sure why you got error.Circumvent
any idea on how to include the application helper in an initializer:: #23036447Lythraceous
A
6

If you are using spork with rspec, you should add the url_helper method to your rspec config -

Anywhere inside '/spec/spec_helper' file:

# spec/spec_helper.rb

RSpec.configure do |config|
  ...
  config.include Rails.application.routes.url_helpers
  ...
end

this loads a built-in ApplicationHelper called "Routes" and calls the '#url_helpers' method into RSpec. There is no need to add it to the ApplicationHelper in '/app/helpers/application_helper.rb' for two reasons:

  1. You are just copying 'routes' functionality into a place that doesn't need it, essentially the controller, which already inherits it from ActionController::Base (I think. maybe ::Metal. not important now). so you are not being DRY - Don't Repeat Yourself

  2. This error is specific to RSpec configuration, fix it where it's broke (My own little aphorism)

Next, I'd recommend fixing your test a little. Try this:

require "spec_helper"

describe ApplicationHelper do
  describe "#link_to_cart" do
    it 'should be a link to the cart' do
     visit cart_path 
     expect(page).to match(/.*href="\/cart".*/)
    end
  end
end

I hope that this is helpful to someone!

Alexei answered 6/7, 2013 at 22:16 Comment(0)
T
0

I was using guard with spring and I found out that, in my case, the issue was caused by spring. After running spring stop it was fixed. But it keeps coming back sometimes when I change something in an ApplicationController.

Testosterone answered 13/3, 2015 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.