i''m trying to use a test dummy to block the interaction between my test and our database. the code im testing makes a handful of active_record calls (create_table, add_index, etc.) i''m trying to use mocks to expect calls at the database adapter level (i''m thinking #execute, but no committed to it yet) long story short, i''m having difficulties telling mocha to expect 2 calls to this method in the same test. thoughts? -- RYAN
On 28/03/2008, Ryan Sobol <ryan.sobol at tobi.com> wrote:> > i''m trying to use a test dummy to block the interaction between my > test and our database. > > the code im testing makes a handful of active_record calls > (create_table, add_index, etc.) i''m trying to use mocks to expect > calls at the database adapter level (i''m thinking #execute, but no > committed to it yet) > > long story short, i''m having difficulties telling mocha to expect 2 > calls to this method in the same test. thoughts?Can you give us some more specifics? Ideally a simple test along with the code that it exercises? Try and keep it as simple as possible while still illustrating the point. It''s always much easier to discuss concrete examples. Thanks. -- James. http://blog.floehopper.org http://tumble.floehopper.org
Examples are always nice, but in the mean time... Model.expects(:save).at_least_once will let you call it one or more times Model.stubs(:save) will let you call it zero or more times But, if you want to test models without hitting the database you can use UnitRecord. If you want to take it another level and test models without loading the Rails framework you can use arbs: http://arbs.rubyforge.org Cheers, Jay On Mar 28, 2008, at 7:30 PM, James Mead wrote:> On 28/03/2008, Ryan Sobol <ryan.sobol at tobi.com> wrote: >> >> i''m trying to use a test dummy to block the interaction between my >> test and our database. >> >> the code im testing makes a handful of active_record calls >> (create_table, add_index, etc.) i''m trying to use mocks to expect >> calls at the database adapter level (i''m thinking #execute, but no >> committed to it yet) >> >> long story short, i''m having difficulties telling mocha to expect 2 >> calls to this method in the same test. thoughts? > > > Can you give us some more specifics? Ideally a simple test along > with the > code that it exercises? Try and keep it as simple as possible while > still > illustrating the point. It''s always much easier to discuss concrete > examples. > > Thanks. > -- > James. > http://blog.floehopper.org > http://tumble.floehopper.org > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer
Other possibilities... Model.expects(:create_table).times(2) # => must be called exactly twice # expect the same call twice Model.expects(:create_table).with(:table_1) # => once with one parameter Model.expects(:create_table).with(:table_2) # => once with another parameter -- James. http://blog.floehopper.org http://tumble.floehopper.org