From: Gary Shea Date: 2005-08-19T23:16:43+09:00 Subject: Re: [ANN] Mockery initial release (yet another dynamic mock object generator) On Fri, 2005-08-19 at 15:59 +0900, Pit Capitain wrote: > Gary Shea schrieb: > > Mockery (http://rubyforge.org/projects/mockery) is a Ruby version of the > > kind of dynamic mock generator that is commonplace in Java. The Java > > versions tend to do a lousy job mocking classes as opposed to > > interfaces, but we don't have that problem in Ruby (not only are there > > no interfaces, but dynamic class modification is SO easy it's just not > > an issue). > > > > A first draft of the above example using Mockery looks like: > > > > ctl = Mockery::Controller.new(Some::Klass) > > ctl.record do |x| > > x.doit('blah') > > end > > ctl.try do |x| > > x.please_call_doit('blah') > > end > > assert_equal(true, ctl.validate, ctl.error_report) > > Nice work! I will try it if I need mocks in my unit tests. One first > question: couldn't you put the last assert into the #try method? > Something like > > def try > ... > ensure > assert_equal(true, validate, error_report) > end > > Regards, > Pit I didn't even know you could do that, very slick. The only problem is that the idea of the #validate and #error_report methods is to make #try independent of the choice of test framework. So we could do what you're saying, but we would have to create a new method: def test_unit_try ... ensure assert(validate, error_report) end and the module that defines that method would have to require 'test/unit' which means a built-in dependency on Test::Unit. Still, it _would_ be convenient to not have to do the assert manually. A dumb-and-obvious way would be to have a subclass Mockery::TestUnitController of Mockery::Controller that just wraps #try as above. Unless I think of or hear a better idea I'll do that for the next release. Thanks! Gary