Ruby on rails 具有多态关联的装置不工作
我正在尝试实现RolifyGem,但在添加具有范围的装置时遇到了问题。下面(模型)测试的最后一行失败,因为目前主持人角色似乎是全局分配给Ruby on rails 具有多态关联的装置不工作,ruby-on-rails,ruby,testing,fixtures,rolify,Ruby On Rails,Ruby,Testing,Fixtures,Rolify,我正在尝试实现RolifyGem,但在添加具有范围的装置时遇到了问题。下面(模型)测试的最后一行失败,因为目前主持人角色似乎是全局分配给@user,而不是只分配给组织一。下面的装置没有使用resource\u id和resource\u type,这在装置的说明中提到,但我不确定如何使用它们。如何将主持人角色的作用域设置为仅组织一 角色.yml moderator: id: 1 resource: one (Organization) moderator: name: :mode
@user
,而不是只分配给组织一。下面的装置没有使用resource\u id
和resource\u type
,这在装置的说明中提到,但我不确定如何使用它们。如何将主持人角色的作用域设置为仅组织一
角色.yml
moderator:
id: 1
resource: one (Organization)
moderator:
name: :moderator
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).
one:
organization: one
roles: moderator, organizations(:one)
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
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
更新:我也尝试了下面的代码。但测试还是失败了 角色.yml
moderator:
id: 1
resource: one (Organization)
moderator:
name: :moderator
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).
one:
organization: one
roles: moderator, organizations(:one)
组织.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
assert_equal @user.has_role?(:moderator, @organization1), true
assert_equal @user.has_role?(:moderator, @organization2), false # This line fails
end
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
我发现,对于更新中的代码,如果我将其作为用户控制器测试或集成测试运行,而不是作为模型测试运行,那么测试确实会通过。所以我想我只是把它当作一种错误的测试