使用rspec测试acts_as_tenant

Mic*_*ale 1 rspec2 ruby-on-rails-3.2 fabrication-gem acts-as-tenant

我有一个使用ascts_as_tenant的多租户应用程序.我正在尝试编写一个测试范围的规范.所以我想测试User.active的输出.

通常,这适用于以下代码

it "returns a correct active user" do
  user_active = Fabricate(:user, curremp: true)
  user_scope2_active = Fabricate(:user, curremp: true)
  user_not_active = Fabricate(:user, account_id: user_active.account_id, curremp: false)
  expect(User.active).should include(user_active)
  #expect(User.active).to_not include(user_scope2_active)
  expect(User.active).to_not include(user_not_active) 
end
Run Code Online (Sandbox Code Playgroud)

但是,我迷失了如何使用acts_as_tenant功能来使用current_tenant,因此验证它只列出来自一个租户的活跃用户.我确信之前已经完成了,但我还没有找到任何文档.

因此,如何使用rspec测试acts_as_tenant用户模型?

Erw*_*inM 6

这就是我们的工作:

通常,您不需要在单元测试中处理向租户提供的信息.因此,只需编写这些测试,就好像没有确定范围一样.

唯一的例外是,如果您想要专门测试与范围有关的内容.我们很少这样做.Acts_as_tenant有自己的测试套件来测试作用域(但是我会说,因为我写了它;).

如果你必须在单元测试中处理AaT,你可以直接设置current_tenant:

ActsAsTenant.current_tenant = Account.first
Run Code Online (Sandbox Code Playgroud)

因此,在后过滤器(将其设置为nil)中清理该租户非常重要,否则您将处于调试状态.