繁体   English   中英

Rails4 Scaffold测试失败,如何找出原因?

[英]Rails4 Scaffold test fails, how can I figure out the reason?

我用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)>'

测试失败有两个原因。 首先,您要获得一个ActiveRecord::Relation而不是从assigns(:posts)返回的Array 其次,在运行此测试时,似乎已经创建了22个帖子。

第一个差异是由于Rails 4对Model.all更改,该更改现在返回ActiveRecord::Relation而不是Array ,如http://edgeguides.rubyonrails.org/4_0_release_notes.html中所述

我无法解释为什么有预先存在的帖子。 基于时间戳,它们似乎是在此测试之前创建的。 在此之前是否还要执行其他创建后的测试? 您关闭交易了吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM