具有多态关联的固定装置不起作用
Fixtures with polymorphic association not working
我正在尝试实现 Rolify gem,但在添加具有适用范围的装置时遇到了问题。下面的(模型)测试的最后一行失败了,因为目前主持人角色似乎是全局赋予 @user
而不是仅针对组织一个。下面的灯具没有使用 resource_id
和 resource_type
,它们在 gem documentation 中提到,但我不确定如何使用它们。 我应该如何将主持人角色的范围设置为仅组织一?
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
我发现使用更新中的代码,如果我 运行 将其作为用户控制器测试或集成测试而不是模型测试,测试确实会通过。所以我想我只是 运行 把它当作错误的测试类型。
我正在尝试实现 Rolify gem,但在添加具有适用范围的装置时遇到了问题。下面的(模型)测试的最后一行失败了,因为目前主持人角色似乎是全局赋予 @user
而不是仅针对组织一个。下面的灯具没有使用 resource_id
和 resource_type
,它们在 gem documentation 中提到,但我不确定如何使用它们。 我应该如何将主持人角色的范围设置为仅组织一?
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
我发现使用更新中的代码,如果我 运行 将其作为用户控制器测试或集成测试而不是模型测试,测试确实会通过。所以我想我只是 运行 把它当作错误的测试类型。