[rspec-users] Mocking Resolv::DNS?

Mikel Lindsaar raasdnil at gmail.com
Tue Jul 24 07:53:18 EDT 2007

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)

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

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

    @dns = mock(Resolv::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?




More information about the rspec-users mailing list