与多态关联不工作日程(Fixtures with polymorphic association n

2019-10-31 04:06发布

我试图实现Rolify宝石却不能增加夹具与它的范围。 的(模型)测试的最后一行,因为目前国内主持人的作用似乎给下面未能@user为组织一个全球性的,而不是只。 下面的灯具不使用resource_idresource_type ,这是中提到的创业板文档的灯具,但我不知道如何使用它们。 我应该如何设置主持人角色只有组织中的一个范围?


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

Answer 1:

我发现,在更新的代码中,测试并如果我运行它作为一个用户控制器测试或集成测试,合格而不是作为一个模型试验。 所以我想我只是运行它的错误类型的测试。



文章来源: Fixtures with polymorphic association not working