我知道我们通常不希望在接受/功能测试中存根方法,但这是我绝对需要为所有接受/功能测试存根的东西.
当我Before在env.rb或Background步骤中将存根调用置于块中时,我收到以下错误.
The use of doubles or partial doubles from rspec-mocks outside of the per-test lifecycle is not supported. (RSpec::Mocks::OutsideOfExampleError)
Run Code Online (Sandbox Code Playgroud)
我应该在哪里放置存根调用,以便它可以在所有情况下工作?
当我试图调查查询的性能问题时,我发现源是这个查询。我正在使用带有 Mongoid gem 的 Rails 4。
Order.where("customer.email" => /\Atest@email.com\z/i)哪里test@email.com只是一个例子。
客户是订单文档中的嵌入文档,客户的电子邮件被索引。
当我使用Benchmark.bmbmwhereOrder.where("customer.email" => /\Atest@email.com\z/i).count重复100次数对性能进行基准测试时,我得到了以下结果。
user system total real
0.090000 0.010000 0.100000 ( 27.656723)
Run Code Online (Sandbox Code Playgroud)
我认为可能\A并且\z导致了缓慢,所以我尝试了以下方法,它查找以给定参数开头的电子邮件:Order.where("customer.email" => /^test/i).count
结果并没有太大的不同。
user system total real
0.090000 0.010000 0.100000 ( 28.712883)
Run Code Online (Sandbox Code Playgroud)
作为最后的手段,我尝试只匹配整个字符串而不使用正则表达式。这一次,它产生了巨大的变化:Order.where("customer.email" => "test@email.com").count
user system total real
0.080000 0.000000 0.080000 ( 0.122888)
Run Code Online (Sandbox Code Playgroud)
当我查看解释的输出时,它表明使用正则表达式扫描所有文档。
{
"cursor" => "BtreeCursor customer.email_1",
"isMultiKey" => false,
"n" => 781,
"nscannedObjects" => 781,
"nscanned" => 500000, …Run Code Online (Sandbox Code Playgroud)