www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mock objects framework...?

reply Robert Fraser <fraserofthenight gmail.com> writes:
A while back someone posted about a mock objects framework they were
developing... is this still in the works/where could I find it?
Jun 29 2007
parent reply Christopher Wright <dhasenan gmail.com> writes:
Robert Fraser wrote:
 A while back someone posted about a mock objects framework they were
developing... is this still in the works/where could I find it?

That was me, and now that I've heard about std.traits, I should be able to create it. Hopefully. Bug me about it in two weeks.
Jun 30 2007
parent reply Mattias Persson <mattias smartcore.se> writes:
Christopher Wright Wrote:

 Robert Fraser wrote:
 A while back someone posted about a mock objects framework they were
developing... is this still in the works/where could I find it?

That was me, and now that I've heard about std.traits, I should be able to create it. Hopefully. Bug me about it in two weeks.

How's it going with the mock framework? Anywhere one could take a look at it?
Nov 05 2007
next sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
Mattias Persson wrote:
 Christopher Wright Wrote:
 
 Robert Fraser wrote:
 A while back someone posted about a mock objects framework they were
developing... is this still in the works/where could I find it?

to create it. Hopefully. Bug me about it in two weeks.

How's it going with the mock framework? Anywhere one could take a look at it?

Thank you. No, it's not working yet; I was stuck on bug 854, though the obvious solution of using a non-anonymous function didn't occur to me. It should work. I'm trying to write a novel this month, so this isn't the best time for programming, though I suspect I would have more success with a mocks library. But with luck, I can get something that sort of works most of the time soon. It will likely not have support for multiple mock repositories or even multiple mocks of the same type. It will likely not have support for mocked objects of one type and real objects of the same type. Not unless I rewrite everything as a giant string mixin. Which would probably turn out better. Keep bothering me, and it'll get done.
Nov 05 2007
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Christopher Wright Wrote:

 It will likely not have support for multiple mock repositories or even 
 multiple mocks of the same type. It will likely not have support for 
 mocked objects of one type and real objects of the same type. Not unless 
 I rewrite everything as a giant string mixin. Which would probably turn 
 out better.
 
 Keep bothering me, and it'll get done.

From my (limited) experience with D metaprogramming, I've found giant string mixins/CTFE functions are more flexible and scalable than templates, at the loss of some readability.
Nov 05 2007
parent Christopher Wright <dhasenan gmail.com> writes:
Robert Fraser wrote:
 Christopher Wright Wrote:
 
 It will likely not have support for multiple mock repositories or even 
 multiple mocks of the same type. It will likely not have support for 
 mocked objects of one type and real objects of the same type. Not unless 
 I rewrite everything as a giant string mixin. Which would probably turn 
 out better.

 Keep bothering me, and it'll get done.

From my (limited) experience with D metaprogramming, I've found giant string mixins/CTFE functions are more flexible and scalable than templates, at the loss of some readability.

Now that I'm using giant string mixins and inheritance rather than code mixins and delegates and redirection, I have some progress. Using inheritance means we have, for the forseeable future: Multiple mocks of the same type. Mocks and real objects of the same type. No final or static methods. No fields. And currently, all we can do is register calls and match them later. No return value setting, currently. No ignoring arguments. No putting in a delegate to execute or check anything. But I can do those relatively quickly. I'm planning on using a fluent interface like the one in Rhino Mocks. (I've also used jMocks, but since it's in Java and uses anonymous classes, it's really weird, and I can't really translate it to D that well.) If you have any suggestions or examples of how you want things to look, let me know. The default (if nobody objects or comes up with a better idea) will be: class IWillBeMockingThis { int foo () { return 0; } void bar (int i) { /* do stuff */ } void dontCallMe (int i, uint j) {} } unittest { MockRepository repository = new MockRepository(); auto m = repository.Mock!(IWillBeMockingThis); Expect(m.foo).Return(31).Repeat(0, 5); /* The following might be useful, but it's low priority. */ Expect(m.bar).Arguments(12); Expect(m.bar(-1)); m.foo; repository.LastCall.Return(3); bool called = false; m.bar(1241); repository.LastCall.Arguments.Any() .Do((int i) { called = true; }); repository.ReplayAll(); /* do stuff */ m.dontCallMe(-3, 12); /* throws an exception */ repository.VerifyAll(); /* throws an exception for each call that has been expected and wasn't called, subject to Repeat. */ } This all has not been implemented yet. But the stuff to create a mock object with an arbitrary payload on each method is there. Given how annoying it will likely be to instantiate a mock repository each test and how much extra typing it could be, I'll probably provide a mock test fixture class with convenience methods for creating mocks and accessing the last call and so forth. I've requested space on dsource.org; I'm hesitant to do anything more until I have a revision control system at my hands.
Nov 06 2007
prev sibling parent Christopher Wright <dhasenan gmail.com> writes:
Mattias Persson wrote:
 Christopher Wright Wrote:
 
 Robert Fraser wrote:
 A while back someone posted about a mock objects framework they were
developing... is this still in the works/where could I find it?

to create it. Hopefully. Bug me about it in two weeks.

How's it going with the mock framework? Anywhere one could take a look at it?

Until it gets a permanent home at dsource: svn co http://damask-mud.googlecode.com/svn/trunk/mocks I did it all in the past two days, so I don't expect it to be terribly useable yet. There are no examples, either. The public interface (so far) is in Mocks.d. I should probably move the unit tests to a separate module. That way it'd be easier to avoid conflicts with the all-powerful main and to keep my unittests from interfering from everyone else's.
Nov 06 2007