【发布时间】:2011-10-28 03:55:16
【问题描述】:
我有一个视图规范正在通过,但现在已将分页(通过 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
【问题讨论】:
-
我知道您已经接受了另一个答案,但我担心通过存根您所做的只是消除了代码中的错误。上面的错误意味着您正在控制器中调用 @posts.current_page 。我可以看到@posts.first.current_page 或controller.current_page 有效,但@posts.current_page 可能不是。
-
好点!那么,我是否应该取消控制器?我不完全确定 Kaminari 期望给
current_page打电话?!?我只知道规范 失败是因为帖子数组没有响应它......但是,你是对的,这看起来很奇怪吗? 我会暂时搁置一下,看看是否有更好的答案。 -
能否也贴一下相关的控制器代码?
标签: ruby-on-rails ruby-on-rails-3 rspec pagination kaminari