1

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

1 回答 1

0

您的测试失败有两个原因。首先,您ActiveRecord::RelationArray. assigns(:posts)其次,在运行此测试时,似乎已经创建了 22 个帖子。

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

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

于 2013-07-08T16:42:16.220 回答