简体   繁体   English

Rails RSpec 随机数据库

[英]Rails RSpec random db

I am trying to figure out RSpec and have some problems.我试图找出 RSpec 并遇到一些问题。 When I run my basic test:当我运行我的基本测试时:

require 'rails_helper'

describe Post do

  before do
    @post = Post.create!(title: 'foobar1', content: 'foobar'*5)
  end

  it 'orders by creation date' do
    @new_post = Post.create!(title: 'foobar1', content: 'foobar'*5)
    Post.order('created_at desc').all.to_a.should == ([@new_post, @post])
  end
end

It looks like I have some more mysterious posts in the db: Failures:看起来我在数据库中有一些更神秘的帖子:失败:

  1) Post orders by creation date
     Failure/Error: Post.order('created_at desc').all.to_a.should == ([@new_post, @post])

       expected: [#<Post id: 980190990, title: "foobar1", content: "foobarfoobarfoobarfoobarfoobar", created_at: "2016-04-19 12:38:50", updated_at: "2016-04-19 12:38:50">, #<Post id: 980190989, title: "foobar1", content: "foobarfoobarfoobarfoobarfoobar", created_at: "2016-04-19 12:38:50", updated_at: "2016-04-19 12:38:50">]
            got: [#<Post id: 980190990, title: "foobar1", content: "foobarfoobarfoobarfoobarfoobar", created_at: "2016-04-19 12:38:50", updated_at: "2016-04-19 12:38:50">, #<Post id: 980190989, title: "foobar1", content: "foobarfoobarfoobarfoobarfoobar", created_at: "2016-04-19 12:38:50", updated_at: "2016-04-19 12:38:50">, #<Post id: 980190962, title: nil, content: nil, created_at: "2016-04-19 11:59:56", updated_at: "2016-04-19 11:59:56">, #<Post id: 298486374, title: nil, content: nil, created_at: "2016-04-19 11:59:56", updated_at: "2016-04-19 11:59:56">] (using ==)
       Diff:
       @@ -1,3 +1,5 @@
        [#<Post id: 980190990, title: "foobar1", content: "foobarfoobarfoobarfoobarfoobar", created_at: "2016-04-19 12:38:50", updated_at: "2016-04-19 12:38:50">,
       - #<Post id: 980190989, title: "foobar1", content: "foobarfoobarfoobarfoobarfoobar", created_at: "2016-04-19 12:38:50", updated_at: "2016-04-19 12:38:50">]
       + #<Post id: 980190989, title: "foobar1", content: "foobarfoobarfoobarfoobarfoobar", created_at: "2016-04-19 12:38:50", updated_at: "2016-04-19 12:38:50">,
       + #<Post id: 980190962, title: nil, content: nil, created_at: "2016-04-19 11:59:56", updated_at: "2016-04-19 11:59:56">,
       + #<Post id: 298486374, title: nil, content: nil, created_at: "2016-04-19 11:59:56", updated_at: "2016-04-19 11:59:56">]

Do You know what is the cause of this problem?你知道这个问题的原因是什么吗?

RSpec usually goes hand in hand in hand with Database Cleaner . RSpec 通常与Database Cleaner齐头并进。

This gem ensures that your database properly resets between tests.这个 gem 确保您的数据库在测试之间正确重置。 Below you can find a standard config script for it.您可以在下面找到它的标准配置脚本。

# spec/rails_helper.rb
require 'database_cleaner'

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do |example|
    DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

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

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