我如何测试Mongoid将生成的查询类型?

时间:2012-06-27 15:23:20

标签: rspec tdd mongoid

我试图断言,使用RSpec,给定的Mongoid查询将加载记录,只检查它的存在,因为记录很大(几MB),代码只需要知道记录是否存在。

我一直在使用关于关联的.exists?,但出于某种原因,这似乎不适用于has_one,例如:

class Profile
  include Mongoid::Document

  has_one :chart
end

class Chart  # this is heavy
  include Mongoid::Document

  belongs_to :profile
end

profile.chart.exists?  # fails if chart returns nil

exists?代理方法显然不适用于has_one关系;虽然记录了has_many。我想制作自己的,但我需要在RSpec中测试确实只记录未加载的记录。我正在考虑做一些事情,比如测试为Mongo驱动程序生成的基础查询,就像使用.to_sql一样。是否有相应的Mongoid方法?

2 个答案:

答案 0 :(得分:0)

profile.chart.nil?    

不起作用?

答案 1 :(得分:0)

您可以在驱动程序中使用command monitoring来检查发送到数据库的实际查询。此方法也适用于不返回任何结果的查询(因为无论如何都会发送查询)。