failing tests fail, passing tests pass.
AngularJS HttpPromise methods (.success and .error) break the promise chain.
It is advisable to stick with classic Promise methods: .then and .catch.
failing tests fail, passing tests pass.
AngularJS HttpPromise methods (.success and .error) break the promise chain.
It is advisable to stick with classic Promise methods: .then and .catch.
| describe("foo", () => { | |
| var $httpBackend; | |
| var $http; | |
| var $q; | |
| var calledFunction = sinon.spy(); | |
| beforeEach(inject((_$httpBackend_, _$http_, _$q_) => { | |
| $httpBackend = _$httpBackend_; | |
| $http = _$http_; | |
| $q = _$q_; | |
| })); | |
| it(`trigger error callback with rejection returned from HttpPromise.success`, () => { | |
| $http.get("/foo") | |
| .success(_ => $q.reject()) | |
| .error(calledFunction); | |
| $httpBackend.expectGET("/foo").respond(200); | |
| $httpBackend.flush(); | |
| expect(calledFunction).to.have.been.called; | |
| }); | |
| it(`chain new promise value with returned promise resolution`, () => { | |
| var value = "a new value"; | |
| $http.get("/foo") | |
| .success(_ => $q.when(value)) | |
| .success(chainedValue => calledFunction(chainedValue)); | |
| $httpBackend.expectGET("/foo").respond(200, "not a new value"); | |
| $httpBackend.flush(); | |
| expect(calledFunction).to.have.been.calledWith(value); | |
| }); | |
| }); |
| module.exports = function (config) { | |
| config.set({ | |
| frameworks: ["mocha", "chai-sinon"], | |
| files: [ | |
| "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js", | |
| "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-mocks.js", | |
| "*.spec.es6", | |
| ], | |
| browsers: ["PhantomJS"], | |
| plugins: [ | |
| "karma-mocha", | |
| "karma-chai-sinon", | |
| "karma-phantomjs-launcher", | |
| "karma-babel-preprocessor", | |
| ], | |
| preprocessors: { | |
| "*.spec.es6": ["babel"], | |
| }, | |
| }); | |
| }; |
| { | |
| "dependencies": { | |
| "chai": "^2.3.0", | |
| "karma": "^0.12.31", | |
| "karma-babel-preprocessor": "^5.2.1", | |
| "karma-chai-sinon": "^0.1.4", | |
| "karma-mocha": "^0.1.10", | |
| "karma-phantomjs-launcher": "^0.1.4", | |
| "mocha": "^2.2.4", | |
| "sinon": "^1.14.1", | |
| "sinon-chai": "^2.7.0", | |
| "babel": "^5.4.7" | |
| } | |
| } |
| describe("foo", () => { | |
| var $httpBackend; | |
| var $http; | |
| var $q; | |
| var calledFunction = sinon.spy(); | |
| beforeEach(inject((_$httpBackend_, _$http_, _$q_) => { | |
| $httpBackend = _$httpBackend_; | |
| $http = _$http_; | |
| $q = _$q_; | |
| })); | |
| it(`trigger error callback with rejection returned from HttpPromise.success`, () => { | |
| $http.get("/foo") | |
| .then(_ => $q.reject()) | |
| .catch(calledFunction); | |
| $httpBackend.expectGET("/foo").respond(200); | |
| $httpBackend.flush(); | |
| expect(calledFunction).to.have.been.called; | |
| }); | |
| it(`chain new promise value with returned promise resolution`, () => { | |
| var value = "a new value"; | |
| $http.get("/foo") | |
| .then(_ => $q.when(value)) | |
| .then(chainedValue => calledFunction(chainedValue)); | |
| $httpBackend.expectGET("/foo").respond(200, "not a new value"); | |
| $httpBackend.flush(); | |
| expect(calledFunction).to.have.been.calledWith(value); | |
| }); | |
| }); |