【问题标题】:Testing acts_as_tenant with rspec使用 rspec 测试acts_as_tenant
【发布时间】:2013-04-18 12:29:38
【问题描述】:

我有一个使用 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

但是,我不知道如何让它使用acts_as_tenant 功能来使用 current_tenant 并因此验证它只列出来自一个租户的活动用户。我确信这已经完成了,但我还没有找到任何文档。

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

【问题讨论】:

    标签: ruby-on-rails-3.2 rspec2 fabrication-gem acts-as-tenant


    【解决方案1】:

    这就是我们所做的:

    通常,您无需在单元测试中处理将信息范围限定给租户的问题。因此,只需编写这些测试,就好像没有范围界定一样。

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

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

    ActsAsTenant.current_tenant = Account.first
    

    那么在后过滤器(将其设置为 nil)中清理该租户很重要,否则您将陷入调试地狱。

    【讨论】:

      【解决方案2】:

      我发现确保在租户发生异常情况时抛出正确的错误消息很有帮助,例如:

          before(:each) do
            ActsAsTenant.current_tenant = FactoryBot.create(:account)
          end
      
          it 'is not valid without an account' do
            foo = FactoryBot.create(:foo)
            expect { foo.account = nil }.to raise_error ActsAsTenant::Errors::TenantIsImmutable
          end
      

      【讨论】:

        猜你喜欢
        • 2014-08-16
        • 2017-06-28
        • 2011-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多