Question

I am trying to stub the results from query to retrieve some records. Here is the query:

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

I am trying to stub it with this:

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

This isn't working. My query to user table looks like this:

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

where it should look like this:

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

What am I doing wrong?

Thanks

Was it helpful?

Solution

so you want to stub this query:

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

i think this should be

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

another thing: it's always a good idea to move finders within methods or scopes in the model. by doing this it is even easier to stub or mock stuff for testing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top