2016-03-30 4 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: Shishirの答えに応じて、以下のようpeople_helper.rbを更新した後に、エラーメッセージは同じままです:

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

(実際にはActiveSupport::SafeBufferですが、あなたは、文字列と同じように扱うことができる)の文字列を返すので、それはあなたの問題ではないcontent_tag方法を試してみてください。

あなたのActiveRecordのインスタンスを使用してヘルパーメソッドを呼び出しているようだ::ベースURLの代わりに:対

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

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

関連する問題