具有多态关联的夹具不起作用 [英] Fixtures with polymorphic association not working

查看:117
本文介绍了具有多态关联的夹具不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现Rolify gem,但是在添加具有作用域的灯具时遇到了麻烦.下面的(模型)测试的最后一行失败,因为当前似乎是全局地将主持人角色分配给了@user,而不仅仅是组织1.下面的装置未使用resource_idresource_type,它们在

I'm trying to implement the Rolify gem but have trouble adding fixtures with a scope for it. The last line of the (model) test below fails because currently the moderator role seems to be given to @user globally instead of only for organization one. The fixtures below aren't using resource_id and resource_type, which are mentioned in the gem documentation for fixtures, but I'm not sure how to use them. How should I set the scope for the moderator role to only organization one?

roles.yml

roles.yml

moderator:
  id: 1
  resource: one (Organization)

users.yml

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


更新:我还尝试了以下代码.但是测试仍然失败.


Update: I also tried the code below. But still the test fails.

roles.yml

roles.yml

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

users.yml

users.yml

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

organizations.yml

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

推荐答案

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

I found out that with the code in the update, the test does pass if I run it as a users controller test or integration test, instead of as a model test. So I guess I was just running it as the wrong type of test.

这篇关于具有多态关联的夹具不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆