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

Wol*_*old 6 tdd rspec 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
Run Code Online (Sandbox Code Playgroud)

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

Hri*_*shi 0

profile.chart.nil?    
Run Code Online (Sandbox Code Playgroud)

不起作用?