0

ヘルパーをテストしようとしていますが、次のエラーが発生します。

Admins::ProductsHelper
  #product_post_permission
    when product can do post
      creates cannot post link with green button (FAILED - 1)
    when product cannot do post
      creates can post link with red button (FAILED - 2)

Failures:

  1) Admins::ProductsHelper#product_post_permission when product can do post creates cannot post link with green button
     Failure/Error: helper.product_post_permission(@product)
     NoMethodError:
       undefined method `cannot_post_admins_product_path' for #<#<Class:0xc64a33c>:0xc65e24c>
     # ./app/helpers/admins/products_helper.rb:6:in `product_post_permission'
     # ./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:0xc64a33c>:0xc66f600>
     # ./app/helpers/admins/products_helper.rb:8:in `product_post_permission'
     # ./spec/helpers/admins/products_helper_spec.rb:24:in `block (4 levels) in <top (required)>'

私はすでに Rails.application.routes.url_helpers を spec_helper.rb に含めましたが、機能していません。

これが私のヘルパーです:

#app/helpers/admins/product_helper.rb
module Admins::ProductsHelper
  def product_post_permission(product)
    html = ""

    if product.can_post?
      html << post_permission_link(product, cannot_post_admins_product_path(product))
    else
      html << post_permission_link(product, can_post_admins_product_path(product), "btn-danger")
    end

    html.html_safe
  end

  private

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

ここに私の仕様があります:

require 'spec_helper'

describe Admins::ProductsHelper do

 describe "#product_post_permission" do
   context "when product can do post" do
     before(:each) do
       @product = FactoryGirl.create(:product)
     end

     it "creates cannot post link with green button" do
       helper.should_receive(:post_permission_link)
       helper.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(:post_permission_link)
       helper.product_post_permission(@product)
     end
   end
 end

end

そしてここに私のルート:

namespace :admins do
  resources :dashboard, only: [:index]
  resources :users,     only: [:show, :edit, :update, :index]
  resources :products,  only: [:show, :edit, :update, :index, :new, :create] do
    member do
      put 'can_post'
      put 'cannot_post'
      put 'publish'
      put 'unpublish'
    end
  end
end

Rails 3.2.13 と Rspec-Rails 2.13 を使用しています。前もって感謝します。

4

0 に答える 0