通过分页打破RSpec测试(Kaminari)

时间:2011-08-16 14:59:28

标签: ruby-on-rails ruby-on-rails-3 rspec pagination kaminari

我有一个视图规格正在传递,但现在已经破坏了分页(通过Kaminari gem)已添加到视图中。我仍然试图了解RSpec的语法...所以寻找帮助来实现这一点,因为页面在浏览器中工作正常。 我知道有很多人对视图规格不屑一顾(因为这样的原因)但是我仍然希望保持这一点

我正在为@posts数组分配一些存根帖子。但是数组不响应current_page。那么我应该如何处理RSpec?

Failures:

  1) posts/index.html.haml renders a list of posts
     Failure/Error: render
     ActionView::Template::Error:
       undefined method `current_page' for #<Array:0x000001028ab4e0>
     # ./app/views/posts/index.html.haml:31:in `_app_views_posts_index_html_haml__291454070937541541_2193463480'
     # ./spec/views/posts/index.html.haml_spec.rb:39:in `block (2 levels) in <top (required)>'

spec/views/posts/index.html.haml_spec.rb

require 'spec_helper'

describe "posts/index.html.haml" do
  before(:each) do
    ...
    assign(:posts, [
      Factory.stub(:post),
      Factory.stub(:post)
    ])    
    view.should_receive(:date_as_string).twice.and_return("June 17, 2011")
    ...
  end

  it "renders a list of posts" do
    render
    rendered.should have_content("June 17, 2011")
    ...
  end
end

2 个答案:

答案 0 :(得分:45)

您还可以执行以下操作:

assign(:posts, Kaminari.paginate_array([
        Factory.stub(:post),
        Factory.stub(:post)
      ]).page(1))

答案 1 :(得分:17)

你应该存根行为,试试这个:

before(:each) do
  ...
  posts = [Factory.stub(:post), Factory.stub(:post)]
  posts.stub!(:current_page).and_return(1)
  posts.stub!(:total_pages).and_return(2)
  assign(:posts, posts)    
  view.should_receive(:date_as_string).twice.and_return("June 17, 2011")
  ...
end