Tasks

Setting up an expectation:

import dmocks.Mocks;

unittest {
   auto mocker = new Mocker;
   auto o = mocker.mock!(MockClass);
   o.method(arguments); // Matches those exact arguments.
   mocker.expect(o.methodWithReturnValue(args)).returns(value);
   
   mocker.replay;
   // test goes here...
   mocker.verify; // throws exception if there are unsatisfied expectations
}

Ignoring arguments:

   o.method(arguments);
   mocker.lastCall().ignoreArgs;
   mocker.expect(o.methodWithReturnValue).ignoreArgs;

Repeating:

   o.method(arguments);
   mocker.lastCall().repeatAny();
   // or, for a range of repetitions:
   mocker.expect(o.methodWithReturnValue).repeat(1, 5);
   mocker.expect(o.methodWithReturnValue).repeat(2);

Throwing an exception:

   o.method(arguments);
   mocker.lastCall().throws(new AbandonedMutexException());

Performing a custom action:

   o.method(arguments);
   // The signature of the delegate must match the signature of
   // the called method.
   mocker.lastCall().action((args) { doSomething(); });
   
   // Non-void method: whatever the delegate returns, the method will
   // return.
   int wallet;
   mocker.expect(o.methodWithReturnValue).action({ return wallet; });
   // ...
   mocker.replay;
   assert (o.methodWithReturnValue() == wallet);
   // ...

Using the original method:

   o.method(arguments);
   // As long as the object is set up correctly (that is, the method doesn't depend on
   // a variable that's initialized in the constructor or another method that wasn't
   // called and passed through), this should do everything the original method does,
   // except with all the same tracking as the rest of the mocked methods.
   mocker.lastCall().passThrough;

Setting up a result without checking it:

   mocker.allowing(o.methodWithReturnValue).returns(12);
   mocker.replay;
   o.methodWithReturnValue;
   o.methodWithReturnValue;
   o.methodWithReturnValue;
   o.methodWithReturnValue;

Not setting up a result for a method:

   mocker.expect(o.methodWithReturnValue); // will throw an exception
   mocker.allowDefaults;
   mocker.expect(o.methodWithReturnValue); // allowDefaults means it'll return the default value

ordered versus unordered

   mocker.ordered;
   o.method(arguments);
   mocker.lastCall().repeat(4);
   mocker.expect(o.methodWithReturnValue).returns(o).repeat(4);

   mocker.unordered;
   mocker.expect(o.toString).returns("wha?").repeatAny;

   mocker.replay;
   o.toString; // okay, it's not ordered

   o.method(arguments); // okay, nothing in the ordered block came before it
   o.methodWithReturnValue; // okay, it's in the right order

   o.method(arguments); // okay, it's got a repeat
   o.toString;
   o.methodWithReturnValue; // okay, the calls from the ordered block have the right relative order

   o.methodWithReturnValue; // Error: expected o.method(arguments) preceding o.methodWithReturnValue, 
                            // but was o.methodWithReturnValue.
   // ...