2016-03-30 50 views
0

我正在爲演示者類編寫測試,並在運行它們時遇到以下錯誤,這些錯誤來自自定義url助手方法。演示者中導致測試錯誤的自定義URL助手方法

Error: 
PersonPresenterTest#test_0011_should get email icon: 
ArgumentError: arguments passed to url_for can't be handled. Please require routes or provide your own implementation 
app/helpers/people_helper.rb:4:in `link_fa_to' 
app/presenters/base_presenter.rb:14:in `method_missing' 
app/presenters/person_presenter.rb:54:in `email_icon' 
test/presenters/person_presenter_test.rb:59:in `block in <class:PersonPresenterTest>' 

person_presenter_test.rb

class PersonPresenterTest < ActionView::TestCase 
    add_template_helper(PeopleHelper) 

    def setup 
    @person = FactoryGirl.create(:person) 
    @presenter = PersonPresenter.new(@person, view) 
    end 

    test 'should get email icon' do 
    expected = '<a class="icon-link" href="/people/1"><i class="fa fa-envelope fa-1x"></i></a>' 
    actual = @presenter.email_icon 
    assert_equal(expected, actual) 
    end 
end 

person_presenter.rb

class PersonPresenter < BasePresenter 
    presents :person 

    def email_icon 
    link_fa_to('envelope fa-1x', person) if person.email.present? 
    end 
end 

base_presenter.rb

class BasePresenter 
    def initialize(object, template) 
    @object = object 
    @template = template 
    end 

    def self.presents(name) 
    define_method(name) do 
     @object 
    end 
    end 

    def method_missing(*args, &block) 
    @template.send(*args, &block) 
    end 
end 

people_helper.rb

module PeopleHelper 

    def link_fa_to(icon_name, link) 
    link_to content_tag(:i, '', class: "fa fa-#{icon_name}"), link, class: "icon-link" 
    end 
end 

的routes.rb

Rails.application.routes.draw do 

    resources :organisations 
    resources :people 
    resources :reset_password, only: [:edit, :update, :new, :index, :create] 
    resources :dashboards, only: [:index] 
    resources :users 

    get 'profile' => 'users#show', as: :profile 

    controller :sessions do 
    get 'login' => :new 
    post 'login' => :create 
    end 

    get '/auth/:provider/callback', to: 'sessions#create' 
    get "sessions/create" 
    get 'sessions' => "sessions#destroy", as: :logout 

    root 'sessions#new' 
end 

我可以測試使用link_to成功演示方法,並期望呼籲link_fa_to爲 '秋' 通過的方法模板以相同的方式。這隻影響測試,該方法在運行應用程序時完美運行。

任何幫助非常感謝。

編輯:

如果我從person_presenter_test.rb刪除add_template_helper(PeopleHelper),錯誤的是:

Error: 
PersonPresenterTest#test_0011_should get email icon: 
NoMethodError: undefined method `link_fa_to' for #<#<Class:0x007fcdb6bb19e0>:0x007fcdb6bb15a8> 
Did you mean? link_to 
app/presenters/base_presenter.rb:14:in `method_missing' 
app/presenters/person_presenter.rb:54:in `email_icon' 
test/presenters/person_presenter_test.rb:58:in `block in <class:PersonPresenterTest>' 

編輯2: 更新people_helper.rb響應以下,以Shishir的回答後,該錯誤消息保持不變:

module PeopleHelper 

    def link_fa_to(icon_name, link) 
    link_to link, class: "icon-link" do 
     content_tag(:i, '', class: "fa fa-#{icon_name}") 
    end 
    end 

end 

回答

1

content_tag返回一個字符串的方法(實際上它是一個ActiveSupport::SafeBuffer,但你可以處理它像一個字符串),所以這不是你的問題。

看來你是用ActiveRecord的實例調用你的helper方法:: Base的,而不是一個網址:

link_fa_to('envelope fa-1x', person) if person.email.present?

VS:

def link_fa_to(icon_name, link) 
    link_to content_tag(:i, '', class: "fa fa-#{icon_name}"), link, class: "icon-link" 
end 

你可以嘗試更新您的代碼如下:

link_fa_to('envelope fa-1x', person_path(person)) if person.email.present?

+0

完美的解釋。我會盡我所能應用賞金。非常感謝 :) – tonyedwardspz

0

您正在使用的link_to語法不接受content_tag作爲參數。它只接受字符串。

link_to content_tag(:i, '', class: "fa fa-#{icon_name}"), link, class: "icon-link" 

嘗試

link_to link do 
    content_tag(:i, '', class: "fa fa-#{icon_name}") 
end 
+0

我想你可能會走在正確的軌道上,但是,我已經提出了你的建議編輯(編輯2),錯誤信息也是一樣的。有任何想法嗎? – tonyedwardspz

+0

你可以更新BasePresenter的代碼嗎?似乎BasePresenter和PersonPersenter代碼在問題中是相同的?你還可以鏈接routes.rb嗎? – Shishir

相關問題