蒙哥大块直到创建? [英] Mongoid block until created?

查看:52
本文介绍了蒙哥大块直到创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Mongoid

I'm creating tests for an API with rspec using Mongoid and FactoryGirl. Mongoid is not a problem in production, but when testing API specs like so:

it "renders [person]" do
  person = FactoryGirl.create(:person)
  get 'persons'
  json.count.should eq(1)
end

正在创建的模型实例(在本例中为Person)有时在API调用运行时未反映在数据库中,从而导致如下错误:

the model instance being created (in this case Person) is sometimes not reflected in the database by the time the API call runs, resulting in an error like so:

 Failure/Error: json.count.should eq(1)

   expected: 1
        got: 0

   (compared using ==)

在知道该条目已反映在数据库中之前,如何创建新的数据库条目并阻止进一步执行该规范?

How can I create new database entries and block further execution of the spec until I know that the entry is reflected in the database?

推荐答案

答案中所述:

为确保您始终可以立即回读刚刚得到的数据 使用Mongoid编写的,您需要设置数据库会话选项

To ensure that you can always immediately read back the data you just wrote using Mongoid, you need to set the database session options

consistency: :strong, safe: true

都不是默认值.

这篇关于蒙哥大块直到创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆