2017-05-31 51 views
0

我已经开始编写一个测试:Rails:为什么我的装置具有相同的ID?

class PostPresenterTest < ActionView::TestCase 
    let(:presenter) { PostPresenter.new(post, view) } 
    let(:post) { Post.first } 

    it 'should something something...' do 
    byebug 
    end 
end 

我有post.yml夹具文件:

one: 
    title: Title One 
    content: First content. 

    two: 
    title: Title Two 
    content: Second content. 

当我放到byebug,我注意到Post.count == 2但个别职位有相同的ID:

Post.first.id == 298486374 
    Post.last.id == 298486374 

这是预期吗?我需要确保每个帖子都有自己的唯一ID。什么是添加ID到固定装置的轨道标准?我应该手动添加ID吗?还是应该采取特定步骤来确保帖子具有不同的ID?

+1

根据[文档](http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html),省略ID应导致自动生成ID。 – max

回答

1

问题是,我认为Post.firstPost.last返回不同的职位。他们实际上返回了相同的帖子。 ID为的是的不同。

(byebug) Post.all 
#<ActiveRecord::Relation [#<Post id: 298486374, title: "Title Two", content: "Second content.", created_at: "2017-06-01 06:44:50", updated_at: "2017-06-01 06:44:50">, #<Post id: 980190962, title: "Title One", content: "First content.", created_at: "2017-06-01 06:44:50", updated_at: "2017-06-01 06:44:50">]> 

我试图Post.order("created_at").firstPost.order("created_at").first但是那些过于返回同一职位。我意识到这是因为这两个帖子都有相同的创建日期。手动添加日期解决了这个问题。 #last#first现在会返回不同的帖子。

相关问题