0

我正在尝试对查询结果进行存根以检索一些记录。这是查询:

ouid = OrganizationalUnit.where("billing_id = ? AND status = 'active'", data[:billing_id]).select(:id)
user_id = User.where("group_id in (14,17) AND organizational_unit_id in (?) AND status = 'active'", ouid).select(:id).first

我试图用这个来存根:

    ou_value = double'fake ou',{ 'id' => '654321' }
    select_double = double 'fake select', {'select' => [ou_value]}
    OrganizationalUnit.stub( :where).and_return(select_double)

这是行不通的。我对用户表的查询如下所示:

SELECT id FROM `users` WHERE (group_id in (14,17) AND organizational_unit_id in ('--- !ruby/object:RSpec::Mocks::Mock\n__null_object: false\nname: fake ou\noptions:\n :__declared_as: Double\n') AND status = 'active') LIMIT 1

它应该看起来像这样:

SELECT id FROM `users` WHERE (group_id in (14,17) AND organizational_unit_id in (654321) AND status = 'active') LIMIT 1

我究竟做错了什么?

谢谢

4

1 回答 1

2

所以你想存根这个查询:

OrganizationalUnit.where("billing_id = ? AND status = 'active'", data[:billing_id]).select(:id)

我认为这应该是

OrganizationalUnit.stub_chain(:where, :select).and_return(654321)

另一件事:在模型的方法或范围内移动查找器总是一个好主意。通过这样做,更容易存根或模拟测试内容。

于 2013-10-17T17:48:49.443 回答