cleishm / jsmockito

Javascript mocking framework inspired by the awesome mockito
http://jsmockito.org
Other
106 stars 19 forks source link

spy object verify is missing "this" function calls #25

Closed Krabi closed 11 years ago

Krabi commented 11 years ago
var likeDao = {
    getA: function(a) {
        return a;
    },
    getB: function(b) {
        return b;
    }
};
var dasSuperService = {
    dao: null,
    getA: function(a) {
        return this.dao.getA(a);
    },
    getB: function(b) {
        return this.dao.getB(b);
    },
    getSum: function() {
        var a = this.getA(1);
        var b = this.getB(2);
        var ret = a + b;
        return ret;
    }
};
// without mock test - calculations are correct!
dasSuperService.dao = likeDao;
assert.equals(3, dasSuperService.getSum());

// Mocked testing
var mockedDao = Mockito.mock(likeDao);
dasSuperService.dao = mockedDao;
var service = Mockito.spy(dasSuperService);

Mockito.when(mockedDao).getA(1).thenReturn(1);
Mockito.when(mockedDao).getB(2).thenReturn(2);

assert.equals(3, service.getSum());

Mockito.verify(service).getSum();
Mockito.verify(mockedDao).getA(1);
Mockito.verify(mockedDao).getB(2);
Mockito.verify(service).getA(1);// Bug
Mockito.verify(service).getB(2);// Bug
cleishm commented 11 years ago

Hi Krabi,

JsMockito can only verify calls made to the spy facade (what is returned from Mockito.spy(object)). Inside the getSum method, this refers to the actual object and not the spy facade - hence calls made in this way will not be recorded for verification.

Cheers, Chris