drewbourne/mockolate — Issue #53: Overriding return values
Author: georgecook
Created: 2012-02-02 19:59:03+00:00
State: open

This isn't currently supported but is very useful for when we write unit tests which have matrix's of data/expected results.

i.e.
var testMatrix : Array = [
{foo: "0", bar:10, mockthis:"5", mockThat:3, expected:Number.POSITIVE_INFINITY}, //FIXME - pretty sure it should not be that!!
{foo: "", bar:10, mockthis:"5", mockThat:3, expected:Number.POSITIVE_INFINITY},
{foo: "1", bar:10, mockthis:"2", mockThat:3, expected:3},
{foo: "1", bar:10, mockthis:"2", mockThat:3, expected:3},
{foo: "5", bar:10, mockthis:"1", mockThat:1, expected:2},
{foo: "2", bar:10, mockthis:"1", mockThat:1, expected:5},
{foo: "2", bar:10, mockthis:"8", mockThat:1, expected:1},
{foo: "2", bar:10, mockthis:"1", mockThat:8, expected:1},
];

for ( var i : int = 0; i < testMatrix.length; i++){
var test : Object = testMatrix[i];
stub(model.thing).method("getFoo").noArgs().returns(test.mockThat);
stub(model.thing).getter("mockThis").noArgs().returns(test.mockThis);
model.bar = test.bar;
data.foo = test.foo;
model.doStuff();
assertThat("test " + i, model.somethingFunky, equalTo(test.expected));
}
//names have been changed to protect the innocent.

I've fixed this locally by updating the following method in mockingCoverture.as -- too busy to do a pull request/blog post, thought others might appreciate this code here, and perhaps you might implement it at some point.

great work - thanks..

pectation(expectation:Expectation):Expectation
{
var currentIndex : int = _expectations.length;
var currentExpectation : Expectation;
var isReplacingExpectation : Boolean;
for (var i : int = 0; i < _expectations.length; i++) {
currentExpectation = _expectations[i] as Expectation;
if ( currentExpectation.name == expectation.name && currentExpectation.invocationType == expectation.invocationType){
currentIndex = i;
isReplacingExpectation = true;
break;
}
}
_expectations[currentIndex] = expectation;

var arrayToUse : Array = _expectationsAsMocks ? _mockExpectations : _stubExpectations;
if ( isReplacingExpectation){
currentIndex = arrayToUse.indexOf(currentExpectation);
} else {
currentIndex = arrayToUse.length;
}
arrayToUse[currentIndex] = expectation;

expectationAdded();

return expectation;
}