【发布时间】:2016-08-26 04:16:09
【问题描述】:
describe('getEmployeeDetails', function () {
var getEmployeeDetails, httpBackend;
//2.
beforeEach(function () {
//3. load the module.
module('nodeApp');
// 4. get your service, also get $httpBackend
// $httpBackend will be a mock.
inject(function ($httpBackend, _getEmployeeDetails_) {
getEmployeeDetails = _getEmployeeDetails_;
httpBackend = $httpBackend;
});
});
// 5. make sure no expectations were missed in your tests.
afterEach(function () {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
//6.
it('ServiceTestSpec', function () {
var returnData = {};
//7. expectGET to make sure this is called once.
httpBackend.expectGET("/employee/getEmployeeDetails").respond(returnData);
//8. make the call.
var returnedPromise = getEmployeeDetails.getEmployees();
//9. set up a handler for the response, that will put the result
// into a variable in this scope for you to test.
var result;
returnedPromise.then(function (response) {
result = response.data;
});
//10. flush the backend to "execute" the request to do the expectedGET assertion.
httpBackend.flush();
//11. check the result.
expect(result).toEqual(returnData);
});
这是我用 karma jasmine 编写的单元测试。 我正在 chrome 控制台中调试这个单元测试,但我得到“响应”对象空白。
我使用 $httpBackend 来伪造 HTTP 请求。这是我没有得到响应对象的原因吗?
但是我的测试通过了成功。 还有一件事是:这是好的做法吗?用angularJS写单元测试?
【问题讨论】:
标签: angularjs node.js karma-runner karma-jasmine