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

I have rspec specification:

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 ApplicationHelper:

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

This works when visiting a site, but the specifications do not work with RuntimeError that routing is unavailable:

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

So, I included Rails.application.routes.urlin my spec, the spec_helperfile, and even myself ApplicationHelper, but to no avail.

Edit: I run tests through spork, maybe this is due to it and causing the problem.

How do I enable these route assistants when working with Spork?

+5
source share
3 answers

You need to add includeat the module level ApplicationHelperbecause ApplicationHelper does not 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

, .

+7

spork rspec, url_helper rspec -

'/spec/spec_helper':

RSpec.configure do |config|
.
. =begin
. bunch of stuff here, you can put the code 
. pretty much anywhere inside the do..end block
. =end
config.include Rails.application.routes.url_helpers
.
. #more stuff
end

ApplicationHelper "" "#url_helpers" RSpec. ApplicationHelper '/app/helpers/application_helper.rb' :

1) "" , , , , ActionController:: Base ( , : "" ). -

2) RSpec, , ( )

, . :

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

, - !

+4

I used guardwith spring, and I found out that in my case, the problem was caused by spring. After launch, spring stopit was fixed. But he sometimes comes back when I change something in ApplicationController.

0
source

All Articles