[rspec-users] Mocking Resolv::DNS?

Pat Maddox
Tue, 24 Jul 2007 12:12:54 -0000

On 7/24/07, Mikel Lindsaar <raasdnil at gmail.com>
wrote:
> 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
>
> --
> http://www.blognow.com.au/q
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>

I''m not sure if this will work, but try

Resolv::DNS.should_receive(:open).and_yield(@dns)

Pat