2008-10-12 93 views
4

我正在寫一個Rails插件,在視圖中構建一個菜單。我使用link_to來建立鏈接,current_page?在當前頁面上設置class="active"如何使用「link_to」和「current_page?」來測試RSpec a Rails插件

我已經include d ActionView::Helpers::UrlHelper所以我可以使用link_to

要獲得current_page?在視圖中工作,我不得不繼承當前類(顯然ActionView::Base),它工作得很好。不幸的是,這完全違背了RSpec的測試。

我打電話link_tocurrent_page?這樣的:

def menu(options = {}, &block) 
    carte = my_menu.new(self) 
    yield carte 
    carte.to_s unless carte.empty? 
end 

class my_menu 
    include ActionView::Helpers::TagHelper 
    include ActionView::Helpers::UrlHelper 

    def initialize(base) 
    @base = base 
    end 

    def link 
    @base.link_to(name, url_options, html_options) 
    @base.current_page?(url_options) 
    end 
end 

而且我得到這個錯誤使用RSpec: 未定義的方法`的link_to」爲#<規格::滑軌::例子:: RailsExampleGroup :: Subclass_1:0x24afa90>

任何線索?

回答

6
include ActionView::Helpers::UrlHelper 

在你的Spec中應該解決問題。或者在你的spec_helper.rb中。我認爲這基本上是默認的視圖規範。