Pat Maddox
2007-Feb-23 21:29 UTC
[rspec-users] How can I spec this? The method gets passed a block...
I''m using Jim Weirich''s Builder library. The code I want to spec is xml.video do xml.id @video_id xml.views @views xml.date(@date.to_s) if @date end I''d like to mock it, rather than asserting that the XML is the right string. I can do one spec: specify "should create a video tag" do @mock_builder.should_receive(:video) do_report end but I can''t do anything else. Setting an expectation for :id, :views, and :date all fail. It''s obvious to me why it does...the stubbed :video method doesn''t know to execute the stuff in the block. So what''s the best way to spec this? Pat
Louis J Scoras
2007-Feb-23 22:16 UTC
[rspec-users] How can I spec this? The method gets passed a block...
On 2/23/07, Pat Maddox <pergesu at gmail.com> wrote:> but I can''t do anything else. Setting an expectation for :id, :views, > and :date all fail. It''s obvious to me why it does...the stubbed > :video method doesn''t know to execute the stuff in the block. So > what''s the best way to spec this?Pat; You can stub the view method in the mock and have it yield so that the block runs: specify "should create a video tag" do @mock_builder.stub!(:video).and_yield @mock_builder.should_receive(:id) do_report end Not really the cleanest solution for this spec since you really shouldn''t care that view was called. That''s why I stubbed it, rather than using should_receive. Not sure how else you could get around it though. -- Lou.
aslak hellesoy
2007-Feb-24 11:48 UTC
[rspec-users] How can I spec this? The method gets passed a block...
On 2/23/07, Pat Maddox <pergesu at gmail.com> wrote:> I''m using Jim Weirich''s Builder library. The code I want to spec is > > xml.video do > xml.id @video_id > xml.views @views > xml.date(@date.to_s) if @date > end > > I''d like to mock it, rather than asserting that the XML is the right > string. I can do one spec: > > specify "should create a video tag" do > @mock_builder.should_receive(:video) > do_report > end > > but I can''t do anything else. Setting an expectation for :id, :views, > and :date all fail. It''s obvious to me why it does...the stubbed > :video method doesn''t know to execute the stuff in the block. So > what''s the best way to spec this? >There is no "best" way to do anything, but in this case I think I would avoid mocking the API I don''t own (builder). I would mock the objects that builder *interacts* with, kick of builder and expect the generated XML to eql some expected XML. Makes sense?> Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Pat Maddox
2007-Feb-24 21:06 UTC
[rspec-users] How can I spec this? The method gets passed a block...
On 2/24/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> On 2/23/07, Pat Maddox <pergesu at gmail.com> wrote: > > I''m using Jim Weirich''s Builder library. The code I want to spec is > > > > xml.video do > > xml.id @video_id > > xml.views @views > > xml.date(@date.to_s) if @date > > end > > > > I''d like to mock it, rather than asserting that the XML is the right > > string. I can do one spec: > > > > specify "should create a video tag" do > > @mock_builder.should_receive(:video) > > do_report > > end > > > > but I can''t do anything else. Setting an expectation for :id, :views, > > and :date all fail. It''s obvious to me why it does...the stubbed > > :video method doesn''t know to execute the stuff in the block. So > > what''s the best way to spec this? > > > > There is no "best" way to do anything, but in this case I think I > would avoid mocking the API I don''t own (builder).I had a feeling when I posted that you would chime in with this very answer :)> I would mock the objects that builder *interacts* with, kick of > builder and expect the generated XML to eql some expected XML.I don''t really follow you here with the mocking. What would I mock? I''m trying to write an object that interacts with Builder, not the other way around. In this case, I guess I should just build out the XML and verify that it''s what I expect. The easiest way to do it is by converting it into a Hash, which I know should work. Something like: specify "should generate the proper XML" do Hash.from_xml(do_report).should == { "video" => { "id" => "1", "views" => "4" } } end How''s that?
aslak hellesoy
2007-Feb-25 09:23 UTC
[rspec-users] How can I spec this? The method gets passed a block...
On 2/24/07, Pat Maddox <pergesu at gmail.com> wrote:> On 2/24/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote: > > On 2/23/07, Pat Maddox <pergesu at gmail.com> wrote: > > > I''m using Jim Weirich''s Builder library. The code I want to spec is > > > > > > xml.video do > > > xml.id @video_id > > > xml.views @views > > > xml.date(@date.to_s) if @date > > > end > > > > > > I''d like to mock it, rather than asserting that the XML is the right > > > string. I can do one spec: > > > > > > specify "should create a video tag" do > > > @mock_builder.should_receive(:video) > > > do_report > > > end > > > > > > but I can''t do anything else. Setting an expectation for :id, :views, > > > and :date all fail. It''s obvious to me why it does...the stubbed > > > :video method doesn''t know to execute the stuff in the block. So > > > what''s the best way to spec this? > > > > > > > There is no "best" way to do anything, but in this case I think I > > would avoid mocking the API I don''t own (builder). > > I had a feeling when I posted that you would chime in with this very answer :) >He he> > I would mock the objects that builder *interacts* with, kick of > > builder and expect the generated XML to eql some expected XML. > > I don''t really follow you here with the mocking. What would I mock? > I''m trying to write an object that interacts with Builder, not the > other way around. >Sorry, I misunderstood what you were trying to do.> In this case, I guess I should just build out the XML and verify that > it''s what I expect. The easiest way to do it is by converting it into > a Hash, which I know should work. > > Something like: > > specify "should generate the proper XML" do > Hash.from_xml(do_report).should == { "video" => { "id" => "1", > "views" => "4" } } > end > > How''s that?Simple, brilliant.> _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >