我使用Post资源生成了脚手架应用程序。
这是测试代码
describe "GET index" do
it "assigns all posts as @posts" do
post = Post.create! valid_attributes
get :index, {}, valid_session
assigns(:posts).should eq([post])
end
end
以下是posts#index
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
似乎是合法的,但是在运行rspec时我不断收到此错误 如果你能给我任何提示,我会很高兴。
Failures:
1) PostsController GET index assigns all posts as @posts
Failure/Error: assigns(:posts).should eq([post])
expected: [#<Post id: 23, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">]
got: #<ActiveRecord::Relation [#<Post id: 1, body: nil, created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 2, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 3, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 4, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 7, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 8, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 9, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 10, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 11, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, #<Post id: 12, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">, ...]>
(compared using ==)
Diff:
@@ -1,2 +1,22 @@
-[#<Post id: 23, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">]
+[#<Post id: 1, body: nil, created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 2, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 3, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 4, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 7, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 8, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 9, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 10, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 11, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 12, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 13, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 14, body: "MyString", created_at: "2013-07-08 15:58:08", updated_at: "2013-07-08 15:58:08">,
+ #<Post id: 15, body: nil, created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">,
+ #<Post id: 16, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">,
+ #<Post id: 17, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">,
+ #<Post id: 18, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">,
+ #<Post id: 19, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">,
+ #<Post id: 20, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">,
+ #<Post id: 21, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">,
+ #<Post id: 22, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">,
+ #<Post id: 23, body: "MyString", created_at: "2013-07-08 15:59:47", updated_at: "2013-07-08 15:59:47">]
# ./spec/controllers/posts_controller_spec.rb:41:in `block (3 levels) in <top (required)>'
答案 0 :(得分:0)
您的测试失败有两个原因。首先,您从ActiveRecord::Relation
获得Array
而不是assigns(:posts)
。其次,在测试运行之前,似乎已经创建了22个帖子。
第一个区别是由于Rails 4更改为Model.all
,现在返回ActiveRecord::Relation
而不是Array
,如http://edgeguides.rubyonrails.org/4_0_release_notes.html中所述。
我无法解释为什么有预先存在的帖子。根据时间戳,它们似乎是在此测试之前创建的。在此之前执行其他创建后测试吗?你关闭了交易吗?