Mocha、Chai、および Sinon を使用して Angular コードをテストしています。
テストする必要がある update という関数にいくつかのコードがあります
function update()
//... Irrelevant code to the question asked
DataService.getEventsData(query).then(function (res) {
var x = res.results;
addTabletoScope(x); // Want to check that this is called.
vm.tableState.pagination.numberOfPages = Math.ceil(res.resultsFound / vm.tableState.pagination.number);
vm.isLoading = false;
vm.count++;
});
そのコードはすべて update 関数内にあります。これはすべて、現在テスト中のコントローラー内にあります。
私のテストで scope.update() を呼び出すとき、scope.addTabletoScope(x) が呼び出されることを確認したいと思います。
テストを実行する前に、私はスパイを持っています
spy = sinon.spy(scope, 'addTabletoScope');
その関数はスコープにバインドされているためです。
これが私が実行した1つのテストです。
it('Expects adding to scope to be called', function(){
$httpBackend.whenPOST(APP_SETTINGS.GODZILLA_EVENTS_URL)
.respond(res.data);
scope.tableState = tableState;
scope.update();
$httpBackend.flush();
expect(spy.called).to.be.true;
})
spy.called が false であるため、これは失敗します。
私が試したもう一つのことは
it('Expects adding to scope to be called', function(){
$httpBackend.whenPOST(APP_SETTINGS.GODZILLA_EVENTS_URL)
.respond(res.data);
scope.tableState = tableState;
scope.update();
scope.$apply();
$httpBackend.flush();
expect(spy.called).to.be.true;
})
これも機能しません。これのどこが間違っているのですか?