簡體   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