Marcelo de Moraes Serpa
2009-Nov-16 03:50 UTC
[rspec-users] Stub a call that happens inside a block
Hello list, I had a hard time to find out a way to mock the singleton feed method of the following class: class RssReader def self.feed(feed_url) output = [] open(feed_url) do |http| response = http.read result = RSS::Parser.parse(response, false) result.items.each_with_index do |item, i| output << {:title => item.title, :description => item.description, :pubDate => item.pubDate, :link => item.link} end unless result.nil? end output end end Since it is being passed a block, I could not intercept the code inside the block, the specific code I''d like to stub is the http.read. I have tried to do: Http.should_receive(:read).and_return(xml) #xml being a fixture, loaded from a file. However, the stub is not applied. Any ideas on how this could be solved? The only way I could think is to avoid using the block... Thanks, Marcelo. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091115/0fbd2a74/attachment.html>
David Chelimsky
2009-Nov-16 04:13 UTC
[rspec-users] Stub a call that happens inside a block
On Sun, Nov 15, 2009 at 7:50 PM, Marcelo de Moraes Serpa < celoserpa at gmail.com> wrote:> Hello list, > > I had a hard time to find out a way to mock the singleton feed method of > the following class: > class RssReader > > def self.feed(feed_url) > output = [] > open(feed_url) do |http| > response = http.read > result = RSS::Parser.parse(response, false) > result.items.each_with_index do |item, i| > output << {:title => item.title, :description => item.description, > :pubDate => item.pubDate, :link => item.link} > end unless result.nil? > end > output > end > end > >Try this: describe "..." do it "..." do http = mock(''http'') RssReader.should_receive(:open).with(''http://this.url'')and_yield(http) http.should_receive(:read) .... ... RssReader.feed(''http://this.url'') end end Make sense? David> Since it is being passed a block, I could not intercept the code inside the > block, the specific code I''d like to stub is the http.read. I have tried to > do: > > Http.should_receive(:read).and_return(xml) #xml being a fixture, loaded > from a file. > > However, the stub is not applied. > > Any ideas on how this could be solved? The only way I could think is to > avoid using the block... > > Thanks, > > Marcelo. > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091115/d3bf399c/attachment-0001.html>
Marcelo de Moraes Serpa
2009-Nov-16 05:05 UTC
[rspec-users] Stub a call that happens inside a block
Yeah, thanks David :) On Sun, Nov 15, 2009 at 10:13 PM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Sun, Nov 15, 2009 at 7:50 PM, Marcelo de Moraes Serpa < > celoserpa at gmail.com> wrote: > >> Hello list, >> >> I had a hard time to find out a way to mock the singleton feed method of >> the following class: >> class RssReader >> >> def self.feed(feed_url) >> output = [] >> open(feed_url) do |http| >> response = http.read >> result = RSS::Parser.parse(response, false) >> result.items.each_with_index do |item, i| >> output << {:title => item.title, :description => item.description, >> :pubDate => item.pubDate, :link => item.link} >> end unless result.nil? >> end >> output >> end >> end >> >> > Try this: > > describe "..." do > it "..." do > http = mock(''http'') > RssReader.should_receive(:open).with(''http://this.url'')and_yield(http) > http.should_receive(:read) .... > ... > RssReader.feed(''http://this.url'') > end > end > > Make sense? > > David > > >> Since it is being passed a block, I could not intercept the code inside >> the block, the specific code I''d like to stub is the http.read. I have tried >> to do: >> >> Http.should_receive(:read).and_return(xml) #xml being a fixture, loaded >> from a file. >> >> However, the stub is not applied. >> >> Any ideas on how this could be solved? The only way I could think is to >> avoid using the block... >> >> Thanks, >> >> Marcelo. >> >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091115/73b78c1b/attachment.html>
Marcelo de Moraes Serpa
2009-Nov-16 05:36 UTC
[rspec-users] Stub a call that happens inside a block
Yes, thanks, David! On Sun, Nov 15, 2009 at 10:13 PM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Sun, Nov 15, 2009 at 7:50 PM, Marcelo de Moraes Serpa < > celoserpa at gmail.com> wrote: > >> Hello list, >> >> I had a hard time to find out a way to mock the singleton feed method of >> the following class: >> class RssReader >> >> def self.feed(feed_url) >> output = [] >> open(feed_url) do |http| >> response = http.read >> result = RSS::Parser.parse(response, false) >> result.items.each_with_index do |item, i| >> output << {:title => item.title, :description => item.description, >> :pubDate => item.pubDate, :link => item.link} >> end unless result.nil? >> end >> output >> end >> end >> >> > Try this: > > describe "..." do > it "..." do > http = mock(''http'') > RssReader.should_receive(:open).with(''http://this.url'')and_yield(http) > http.should_receive(:read) .... > ... > RssReader.feed(''http://this.url'') > end > end > > Make sense? > > David > > >> Since it is being passed a block, I could not intercept the code inside >> the block, the specific code I''d like to stub is the http.read. I have tried >> to do: >> >> Http.should_receive(:read).and_return(xml) #xml being a fixture, loaded >> from a file. >> >> However, the stub is not applied. >> >> Any ideas on how this could be solved? The only way I could think is to >> avoid using the block... >> >> Thanks, >> >> Marcelo. >> >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091115/b049e8fb/attachment.html>