Hi all, consider a class Foo that send, in its constructor, some message to an object of class Bar: class Foo def initialize(bar) @bar = bar @bar.some_message end ... end Now, in order to test Foo, I''d like to decouple it from Bar mocking bar object, so: describe Foo do before do @bar = mock(''bar'') @bar.should_receive(:some_message) @foo = Foo.new(bar) end it ''should ...'' it ''should ...'' ... end The question is: is it appropriate to put a mock expectation inside a before block? Or mock expectations are relegated to example blocks? Thanks in advance. Andrea
Hi Andrea, I generally put stub! calls in the before block and then have the mock expectation in the example block. -- Matt Berther http://www.mattberther.com On Apr 29, 2008, at 5:59 AM, Andrea Fazzi wrote:> Hi all, > > consider a class Foo that send, in its constructor, some message to > an object of class Bar: > > class Foo > def initialize(bar) > @bar = bar > @bar.some_message > end > > ... > > end > > Now, in order to test Foo, I''d like to decouple it from Bar mocking > bar object, so: > > describe Foo do > > before do > @bar = mock(''bar'') > @bar.should_receive(:some_message) > @foo = Foo.new(bar) > end > > it ''should ...'' > it ''should ...'' > > ... > > end > > The question is: is it appropriate to put a mock expectation inside > a before block? Or mock expectations are relegated to example blocks? > > Thanks in advance. > Andrea > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Matt Berther ha scritto:> Hi Andrea, > > I generally put stub! calls in the before block and then have the mock > expectation in the example block. > > -- > Matt Berther > http://www.mattberther.com > > > > > On Apr 29, 2008, at 5:59 AM, Andrea Fazzi wrote: > >> Hi all, >> >> consider a class Foo that send, in its constructor, some message to >> an object of class Bar: >> >> class Foo >> def initialize(bar) >> @bar = bar >> @bar.some_message >> end >> >> ... >> >> end >> >> Now, in order to test Foo, I''d like to decouple it from Bar mocking >> bar object, so: >> >> describe Foo do >> >> before do >> @bar = mock(''bar'') >> @bar.should_receive(:some_message) >> @foo = Foo.new(bar) >> end >> >> it ''should ...'' >> it ''should ...'' >> >> ... >> >> end >> >> The question is: is it appropriate to put a mock expectation inside a >> before block? Or mock expectations are relegated to example blocks? >> >> Thanks in advance. >> Andrea >> >> >> _______________________________________________ >> 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 >Thank you for your answer Matt. I think I''ve cought the point. Stub methods return canned responses so they are not intended to underline interactions between the mock object and the object under testing. Thank you again. Andrea