[rspec-users] Mocking Resolv::DNS?

Mikel Lindsaar
Tue, 24 Jul 2007 11:53:18 -0000

Hello Rspecers,

I have a rails project where I am calling Resolv::DNS.open and then
using the block to check a domain name.

The code snippet in question is:

   domain = "mytest.com"
   Resolv::DNS.open do |dns|
     @mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
   end

I obviously want to stub this out, especially for speed but can''t
quite work out how.

I have done:

>From what I can tell, Resolv::DNS first is receiving :open and it will
then be yielding instances of Resolv::DNS to be using inside the
block.

So, my last unsuccessfull attempt at mocking this out is:

    @dns = mock(Resolv::DNS)
    Resolv::DNS.should_receive(:open).and_return([@dns])
    @dns.should_receive(:getresources).and_return("MX Record")

The errors I am getting are not about mocks receiving unexpected
messages.  The :open message mock gets called.  The second one (on
@dns.should_receive(:getresources) also does not complain.

But @mx in the above code ends up as nil, when I would think it should
end up as "MX Record".

Any pointers?

Regards

Mikel

--