How to get DS.find().then() to be called when using angular-data-mocks

164 Views Asked by At

I've been trying to test a service I'm writing that makes use of angular-data data stores and can't get it to call the callback passed to then(). I've got a basic example of what I'm trying to do below and what I expect to happen. Can anybody shed any light on what it is I'm doing wrong and why this doesn't work as expected?

I'd expect this test to pass and print this is called to the console. It does neither, failing with "expected false to be true".

describe('usage of expectFind', function(){
    beforeEach(function (done) {
        inject(function (_DS_) {
            DS = _DS_;
            done();
        });
    });

    it('should call the callback passed to then', function(done){
        DS.expectFind('foo', 1).respond([{a: 'thing'}])

        var called = false;
        DS.find('foo', 1).then(function(aFoo){
            console.log('this is called');
            called = true;
            done();
        });
        expect(called).toBe(true);
    });
});
1

There are 1 best solutions below

0
redrah On

OK we worked it out, we need to call DS.flush() before the expectation. This is what actually triggers the callbacks to be invoked.

describe('usage of expectFind', function(){
beforeEach(function (done) {
    inject(function (_DS_) {
        DS = _DS_;
        done();
    });
});

it('should call the callback passed to then', function(done){
    DS.expectFind('foo', 1).respond([{a: 'thing'}])

    var called = false;
    DS.find('foo', 1).then(function(aFoo){
        console.log('this is called');
        called = true;
        done();
    });


    // this is what's missing
    DS.flush();

    expect(called).toBe(true);
});

});