具有多态关联的灯具不起作用

时间:2015-06-29 16:22:17

标签: ruby-on-rails ruby testing fixtures rolify

我正在尝试实现Rolify gem但是无法添加具有范围的灯具。下面(模型)测试的最后一行失败,因为目前主持人角色似乎全局授予@user,而不是仅针对组织1。下面的灯具没有使用resource_idresource_typegem documentation中提到了灯具,但我不确定如何使用它们。 我应该如何将主持人角色的范围设置为仅限组织1?

roles.yml

moderator:
  id: 1
  resource: one (Organization)

users.yml里

one:
  email: example@example.com
  roles: moderator, organizations(:one)           # I was hoping this would set the scope of the role to organization one but it isn't (seems to set the role globally).

test.rb

def setup
  @moderator_role = roles(:moderator)
  @organization1  = organizations(:one)
  @organization2  = organizations(:two)
  @user           = users(:one)
end

test "should be moderator if fixtures correct" do 
  assert_equal @user.has_role?('moderator'), true
  assert_equal @user.has_role?(:moderator, @organization1), true
  assert_equal @user.has_role?(:moderator, @organization2), false       # This line fails
end

更新:我也尝试过以下代码。但测试仍然失败。

roles.yml

moderator:
  name: :moderator
  resource: one (Organization)

users.yml里

one:
  organization: one
  roles: moderator, organizations(:one)

organizations.yml

one:
  name: "Company A"

test.rb

def setup
  @moderator_role = roles(:moderator)
  @organization1  = organizations(:one)
  @organization2  = organizations(:two)
  @user           = users(:one)
end

test "should be moderator if fixtures correct" do 
  assert_equal @user.has_role?('moderator'), true                      # This line fails
  assert_equal @user.has_role?(:moderator, @organization1), true       # This line also fails
  assert_equal @user.has_role?(:moderator, @organization2), false
end

1 个答案:

答案 0 :(得分:0)

我发现使用更新中的代码,如果我将其作为用户控制器测试或集成测试运行,而不是作为模型测试,则测试确实通过。所以我想我只是将它作为错误的测试类型运行。