Skip to content

URL Helpers not working even with Rails.application.routes.url_helpers included on spec_helper #725

@edelpero

Description

@edelpero

Hello, I'm having trouble testing a custom helper. I already included Rails.application.routes.url_helpers on spec_helper.rb, but doesn't work either. Here are all the details of my code:

################################################
#app/helpers/admins/products_helper.rb

module Admins::ProductsHelper
  def product_post_permission(product)
    html = ""

    if product.can_post?
      html << product_action_link(product, cannot_post_admins_product_path(product),
                                "can_post", "btn-success", "icon-lock")
    else
      html << product_action_link(product, can_post_admins_product_path(product),
                                "can_post", "btn-danger", "icon-lock")
    end

    html.html_safe
  end

  def product_published_status(product)
    html = ""

    if product.published?
      html << product_action_link(product, unpublish_admins_product_path(product),
                                "published", "btn-success", "icon-eye-open")
    else
      html << product_action_link(product, publish_admins_product_path(product),
                                "published", "btn-danger", "icon-eye-close")
    end

    html.html_safe
  end

  private

    def product_action_link(product, path, element, button_type, icon)
      link_to path,
              method: :put,
              remote: true,
              id:     dom_id(product, element),
              class: "btn btn-small #{button_type}" do
                "<i class='icon #{icon} icon-white'></i>".html_safe
      end
    end
end

################################################
#spec/helpers/admins/products_helper.rb

require 'spec_helper'

describe Admins::ProductsHelper do

  describe "#product_post_permission" do
    context "when product can do post" do
      before(:each) do
        @product = mock_model(Product, can_post?: true)
      end

      it "creates cannot post link with green button" do
        puts "helper defined" if defined?(Rails.application.routes.url_helpers)
        helpers.should_receive(:product_action_link)
        helpers.product_post_permission(@product)
      end
    end

    context "when product cannot do post" do
      before(:each) do
        @product = mock_model(Product, can_post?: false)
      end

      it "creates can post link with red button" do
        helper.should_receive(:product_action_link)
        helper.product_post_permission(@product)
      end
    end
  end

  describe "#product_published_status" do
    context "when product is published" do
      before(:each) do
        @product = mock_model(Product, published?: true)
      end

      it "creates unpublish link with green button" do
        helper.should_receive(:product_action_link)
        helper.product_published_status(@product)
      end
    end

    context "when product is not published" do
      before(:each) do
        @product = mock_model(Product, published?: false)
      end

      it "creates publish link with red button" do
        helper.should_receive(:product_action_link)
        helper.product_published_status(@product)
      end
    end
  end

end

################################################
#spec/spec_helper.rb

RSpec.configure do |config|
  config.mock_with :rspec
  config.include   Devise::TestHelpers, :type => :controller
  config.include   Paperclip::Shoulda::Matchers
  config.include   Rails.application.routes.url_helpers
end

################################################
#error messages:

Admins::ProductsHelper
  #product_post_permission
    when product can do post
helper defined
      creates cannot post link with green button (FAILED - 1)
    when product cannot do post
      creates can post link with red button (FAILED - 2)
  #product_published_status
    when product is published
      creates unpublish link with green button (FAILED - 3)
    when product is not published
      creates publish link with red button (FAILED - 4)

Failures:

  1) Admins::ProductsHelper#product_post_permission when product can do post creates cannot post link with green button
     Failure/Error: helpers.should_receive(:product_action_link)
     RuntimeError:
       In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers
     # ./spec/helpers/admins/products_helper_spec.rb:13:in `block (4 levels) in <top (required)>'

  2) Admins::ProductsHelper#product_post_permission when product cannot do post creates can post link with red button
     Failure/Error: helper.product_post_permission(@product)
     NoMethodError:
       undefined method `can_post_admins_product_path' for #<#<Class:0xbf55180>:0xbf60378>
     # ./app/helpers/admins/products_helper.rb:9:in `product_post_permission'
     # ./spec/helpers/admins/products_helper_spec.rb:25:in `block (4 levels) in <top (required)>'

  3) Admins::ProductsHelper#product_published_status when product is published creates unpublish link with green button
     Failure/Error: helper.product_published_status(@product)
     NoMethodError:
       undefined method `unpublish_admins_product_path' for #<#<Class:0xbf55180>:0xbf71ccc>
     # ./app/helpers/admins/products_helper.rb:20:in `product_published_status'
     # ./spec/helpers/admins/products_helper_spec.rb:38:in `block (4 levels) in <top (required)>'

  4) Admins::ProductsHelper#product_published_status when product is not published creates publish link with red button
     Failure/Error: helper.product_published_status(@product)
     NoMethodError:
       undefined method `publish_admins_product_path' for #<#<Class:0xbf55180>:0xa1fec08>
     # ./app/helpers/admins/products_helper.rb:23:in `product_published_status'
     # ./spec/helpers/admins/products_helper_spec.rb:49:in `block (4 levels) in <top (required)>'

Finished in 0.08698 seconds
4 examples, 4 failures

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions