/++ An example of using mock objects to test a class. Demonstrates the IgnoreArguments option. ++/ /** Standard interface. **/ interface IAssociate (T) { uint calculate (ubyte[] data); bool isStored (T item); void store (T item); } class ToTest { private IAssociate!(real) _associate; public this (IAssociate!(real) associate) { _associate = associate; } uint calculate (real number, ubyte[] data) { if (!_associate.isStored(number)) { _associate.store(157.2342); return _associate.calculate(data); } else { return 0; } } unittest { // The mocker, as always. auto mock = new Mocker; // And the setup of data. auto associate = mock.mock!(IAssociate!(real)); real number = 7.23; ubyte[] data = new ubyte[4]; uint result = 42; // I don't care this time what it's expecting to have stored, or what it tries to // store, so let's ignore it. The order of these methods doesn't matter; I could // have put IgnoreArguments before Return(false). mock.expect(associate.isStored(number)).returns(false).ignoreArgs; // Again, pay no attention to the arguments. associate.store(number); mock.lastCall().ignoreArgs; // Here we still care about the arguments. mock.expect(associate.calculate(data)).returns(result); // And continue with the rest of the test. mock.replay; auto target = new ToTest(associate); assert (target.calculate(number, data) == result); mock.verify; } }
back to Examples
