Ben Aston
Ben Aston

Reputation: 55769

Jasmine - how do I create a pure stub with all methods stubbed

In Jasmine how do I create a pure stub with all methods stubbed and returning undefined?

Upvotes: 0

Views: 177

Answers (1)

josh.bradley
josh.bradley

Reputation: 79

I don't think there is anything out of the box that does this but you could create your own.

describe('Stub all', function(){
var stubAll = function(obj){
    for(propt in obj){
        if(typeof obj[propt]  === 'function')
            spyOn(obj, propt).and.returnValue(undefined)
    }
}

var underTest = {
  thing: {},
    foo: function(){
        return 'bar';
    },
    bar: function(){
        return 'foo';
    }
};

it('should return undefined for all functions.', function(){
    stubAll(underTest);

    expect(underTest.foo()).toEqual(undefined);
    expect(underTest.bar()).toEqual(undefined);
});
});

The stubAll function will add a spy to each function on the object passed in that will return undefined.

Upvotes: 2

Related Questions