我正在尝试测试帮助程序,但出现以下错误:
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)>'
我已经在我的 spec_helper.rb 中包含了 Rails.application.routes.url_helpers 但它不起作用。
这是我的助手:
#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。提前致谢。