2011-12-03 131 views
3

對於我的視圖,我有一個rspec測試,在我使用draper抽取某些邏輯之前,它工作正常。該視圖在瀏覽器中呈現正確,但現在測試失敗。使用rspec測試Draper增強模型使用rspec測試Draper增強模型

require 'spec_helper' 

describe "articles/index.html.erb" do 
    before(:each) do 
    assign(:articles, [ 
     stub_model(Article, 
     :title => "Title", 
     :slug => "this-is-a-slug", 
     :content => "Content", 
     :excerpt => "Excerpt", 
     :starts_at => "2012-01-10 01:00:00", 
     :ends_at => "2012-01-10 01:30:00", 
     :show_times => true 
    ) 
    ]) 
    render 
    end 

    it "renders a list of articles" do 
    assert_select "h3", :text => "Title".to_s, :count => 8 
    assert_select "div.article_body", :text => "Content".to_s, :count => 8 
    end 
end 

該視圖包含的裝飾方法calendars,這在裝飾/ article_decorator.rb定義的電話,但因爲我使用stub_model rspec的不撿這件事,大概是:

1) articles/index.html.erb renders a list of articles 
    Failure/Error: render 
    ActionView::Template::Error 
    undefined method `calendars' for #<Article:0x007fd41c68be90> 
    # ./app/views/articles/index.html.erb:4:in `block in _app_views_articles_index_html_erb___1865228919968458103_70274492660680' 
    # ./app/views/articles/index.html.erb:1:in `each' 
    # ./app/views/articles/index.html.erb:1:in `_app_views_articles_index_html_erb___1865228919968458103_70274492660680' 
    # ./spec/views/articles/index.html.erb_spec.rb:85:in `block (2 levels) in <top (required)>' 

我如何使我的測試再次工作? (或者是有測試我的意見的更好的辦法?)

回答

1

我還沒有開始使用德雷珀,但是我想這應該工作:

assign(:articles, [ 
    ArticleDecorator.new(
     stub_model(Article, 
     :title => "Title", 
     :slug => "this-is-a-slug", 
     :content => "Content", 
     :excerpt => "Excerpt", 
     :starts_at => "2012-01-10 01:00:00", 
     :ends_at => "2012-01-10 01:30:00", 
     :show_times => true 
     ) 
    )]) 

現在的文章是一個裝飾器數組,誰迴應到calendars方法。