首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用rspec测试Draper增强的模型

使用rspec测试Draper增强的模型
EN

Stack Overflow用户
提问于 2011-12-03 22:20:52
回答 1查看 959关注 0票数 3

我对我的视图进行了rspec测试,在我使用draper抽象出一些逻辑之前,它工作得很好。视图在浏览器中正确呈现,但测试现在失败。

代码语言:javascript
复制
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的调用,该方法在装饰器/文章_装饰器.rb中定义,但rspec没有采用此方法,可能是因为我使用的是stub_model:

代码语言:javascript
复制
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)>'

我怎样才能让我的测试再次工作?(或者有没有更好的方法来测试我的视图?)

EN

回答 1

Stack Overflow用户

发布于 2012-01-25 19:03:02

我还没有开始使用Draper,但我想这应该是可行的:

代码语言:javascript
复制
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方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8368287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档