Rspec,预计范围变化

Kon*_*rad 4 scope rspec ruby-on-rails

我在 Rspec 中编写了以下测试:

expect {
         ...
        }.to change( User.where(:profile.exists => true), :count ).by(1)
Run Code Online (Sandbox Code Playgroud)

但此作用域仅执行一次,并且始终是具有相同大小的相同数组。如何让rspec在expect中运行代码之前和之后执行这个范围?

Tod*_*obs 5

OPs 解决方案,作为答案发布

这对于有类似问题的其他人可能有效,也可能无效。原问题中未包含语料库,且未经独立验证。

expect {
  # test goes here
}.to change{ User.where(:profile.exists => true).count }.by(1)
Run Code Online (Sandbox Code Playgroud)