好 - 回答我自己的问题(回想起来) - “看着你的六个人”;这意味着过去的设置,定义和微小的“补丁”可能会偷偷摸摸!
在我的情况下,一个环境变量帮助我默认范围我的所有模型,是罪魁祸首!
这个gist记录了我试图证明完全相同的测试在rails控制台中传递,当启动
时OX_ID=1 RAILS_ENV=test r c
在使用 rake spec 运行工厂时失败,提供此输出:
Running spec/models/user_spec.rb
....F
Failures:
1) User should have 1 role through user_group
Failure/Error: permission = Factory.create( :roleable, authorization: "all", ox: @ox, role: role )
ActiveRecord::RecordInvalid:
Validering fejlede: Ox skal udfyldes
# ./spec/factories/factories.rb:28:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:60:in `block (2 levels) in <top (required)>'
Finished in 2.01 seconds
5 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:56 # User should have 1 role through user_group
作为Rspec测试的全新人,并且对BTW不断变得越来越高的整个Rails堆栈感到不舒服,我完全错过了命令行上的ENV [“OX_ID”],并没有幸存下来测试环境(原因,为什么/应该如何?可能吗?)
取消此
我终于添加了
y user.roles.to_sql
到我的测试(让我看看哪种SQL Rails编译) - 当我看到输出时我不相信自己的眼睛!
SELECT `roles`.* FROM `roles` INNER JOIN `roleables` ON `roles`.`id` = `roleables`.`role_id`
INNER JOIN `user_groups` ON `roleables`.`roleable_id` = `user_groups`.`id` AND `roleables`.`roleable_type`
= 'UserGroup' INNER JOIN `user_groups_users` ON `user_groups`.`id` = `user_groups_users`.`user_group_id`
WHERE `user_groups_users`.`user_id` = 3 AND (roles.ox_id=NULL) AND (roleables.ox_id=NULL)
AND (user_groups.ox_id=NULL)
(roles.ox_id=NULL)
全部放弃了!我原以为它会说roles.ox_id=1
而且在这里!
答案 0 :(得分:0)
您正在开发环境中的控制台中运行rspec / shoulda代码,其中未加载库。我不确定你会怎么做,但如果你想在控制台上做一个快速测试,你可以打印出来:
user.user_groups[0].roles.count == 1
在这种情况下,保持简单可能会更好。希望有所帮助。