Ruby on rails 失败/错误:分配(:post)。是否应eq([post])
后控制器规格rbRuby on rails 失败/错误:分配(:post)。是否应eq([post]),ruby-on-rails,ruby,ruby-on-rails-3,rspec,capybara,Ruby On Rails,Ruby,Ruby On Rails 3,Rspec,Capybara,后控制器规格rb require 'spec_helper' describe Blog::PostsController do let!(:posts) { FactoryGirl.create_list(:post, 3) } let!(:post) { posts.first } describe "GET index" do it "renders the :index view" do get :index assigns(:posts).s
require 'spec_helper'
describe Blog::PostsController do
let!(:posts) { FactoryGirl.create_list(:post, 3) }
let!(:post) { posts.first }
describe "GET index" do
it "renders the :index view" do
get :index
assigns(:posts).should eq([post])
response.should render_template :index
end
end
describe "GET show" do
context "invalid post" do
before do
get :show, :id => 99
end
it "redirects to the 404 page" do
response.should render_template(:file => "#{Rails.root}/public/404.html")
end
end
context "valid post" do
it "show page" do
get :show, id: post
assigns(:post).should eq(post)
response.should render_template :show
end
end
end
end
这是我的索引操作:
def index
@posts = Post.all.order_by(created_at: :desc).page(params[:page])
respond_to do |format|
format.html
end
end
我得到了这个错误:
1) Blog::PostsController GET index renders the :index view
Failure/Error: assigns(:posts).should eq([post])
expected: [#<Post _id: 517ebb98a616542f41000002, created_at: 2013-04-29 18:27:36 UTC, updated_at: 2013-04-29 18:27:36 UTC, image_filename: nil, impressions_count: nil, tags: ["tag1", "tag2", "tag3", "tag4"], _slugs: ["post-number-1"], title: "Post number 1", content: "[\"Maiores dolor illum distinctio eveniet perspiciatis necessitatibus consequatur. Dicta ratione repellat ullam sit sed inventore voluptatem. Possimus magni cum dolores rerum voluptas quibusdam. Sed rerum atque accusantium amet aut.\", \"Consequatur ab eum voluptatem voluptatem sit et. Natus soluta quam sed quasi vel odio assumenda. Nulla excepturi dicta voluptatem voluptas vel sit.\"]", image: nil, image_cache: nil, remove_image: nil, admin_id: "517ebb98a616542f41000001">]
got: #<Mongoid::Criteria
selector: {}
options: {:sort=>{"created_at"=>-1}, :limit=>9, :skip=>0}
class: Post
embedded: false>
(compared using ==)
Diff:
@@ -1,2 +1,6 @@
-[#<Post _id: 517ebb98a616542f41000002, created_at: 2013-04-29 18:27:36 UTC, updated_at: 2013-04-29 18:27:36 UTC, image_filename: nil, impressions_count: nil, tags: ["tag1", "tag2", "tag3", "tag4"], _slugs: ["post-number-1"], title: "Post number 1", content: "[\"Maiores dolor illum distinctio eveniet perspiciatis necessitatibus consequatur. Dicta ratione repellat ullam sit sed inventore voluptatem. Possimus magni cum dolores rerum voluptas quibusdam. Sed rerum atque accusantium amet aut.\", \"Consequatur ab eum voluptatem voluptatem sit et. Natus soluta quam sed quasi vel odio assumenda. Nulla excepturi dicta voluptatem voluptas vel sit.\"]", image: nil, image_cache: nil, remove_image: nil, admin_id: "517ebb98a616542f41000001">]
+#<Mongoid::Criteria
+ selector: {}
+ options: {:sort=>{"created_at"=>-1}, :limit=>9, :skip=>0}
+ class: Post
+ embedded: false>
# ./spec/controllers/blog/posts_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
1)Blog::PostsController GET index呈现:index视图
失败/错误:分配(:post)。是否应eq([post])
期望值:[#]
获取:#{“已在”=>-1}、:limit=>9、:skip=>0}处创建
类别:邮政
嵌入:false>
(使用==进行比较)
差异:
@@ -1,2 +1,6 @@
-[#]
+#{“创建于”=>-1},:限制=>9,:跳过=>0}
+类别:邮政
+嵌入:false>
#./spec/controllers/blog/posts\u controller\u spec.rb:8:in'block(3层)in'
我的表演动作,通过测试。索引操作的错误在哪里?这是因为您试图比较不同类型的对象 您可以尝试如下方式转换
assings[:posts]
:
assings[:posts].to_a.should eq([post])
但是由于数组不同,我认为它也不会工作
因此,您可以尝试验证post
是否在assings[:post]
中:
assings[:posts].should include(post)
Mongoid在绝对必要时才会获取数据。这意味着,当你做
Model.where
时,你会得到一个对象。此类实现了计数
和每个
(还包括可枚举
),因此它的行为通常类似于数组
,但它不是。您需要使用将其转换为a
这将解决以下错误:
describe "GET index" do
it "renders the :index view" do
get :index
assigns(:posts).to_a.should match_array posts
response.should render_template :index
end
end
显示操作之所以有效,是因为
Model.find
和Model.find\u by
将返回单个对象,如果该对象不存在,则引发异常;所以Mongoid立即运行查询。谢谢,但是使用分配[:posts]。应该包括(post)
,我得到了相同的错误谢谢,现在,我得到了一个额外元素的错误:额外元素是:[啊,对了,我看到你让FactoryGirl创建了3篇帖子。这很有意义。更新了我的答案。