Rails4 脚手架测试失败,我怎么能找出原因



>我用 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 中所述。

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

最新更新