2015-01-26 45 views
0

我一直在試圖測試我正在寫的服務,它利用了角度數據數據存儲,讓它調用傳遞給then()的回調。我已經有了一個基本的例子,說明了我想要做的以及我期望發生的事情。任何人都可以闡明我做錯了什麼,爲什麼這不能按預期工作?如何在使用角度數據模擬時調用DS.find()。then()

我希望這個測試通過並打印this is called到控制檯。它既沒有,也沒有「預期錯誤是真實的」。我們的工作了

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); 
    }); 
}); 
+0

此外,我試圖用'angular-data-mocks'標籤添加它,但它不存在,我沒有足夠的代表來創建它,如果有人可以添加它,那將是很棒的。 – redrah 2015-01-26 18:30:57

回答

0

OK,我們需要期待之前調用DS.flush()。這實際上觸發了被調用的回調。

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); 
}); 

});

相關問題