[英]Jasmine unit test AngularJS factory with two dependencies ($http and another factory returning promise)
I am using Ionic framework for custom applications. 我正在使用Ionic框架进行自定义应用程序。 In the process, I am trying to write Unit test for the factory datastoreServices
which has a dependency on DomainService
and $http
. 在这个过程中,我试图为工厂datastoreServices
编写单元测试,它依赖于DomainService
和$http
。 I am kind a confused on the implementation of Jasmine Unit tests. 我对Jasmine Unit测试的实现感到困惑。
My factories are as follows. 我的工厂如下。
app.factory("datastoreServices", ["$http", function($http) {
return {
getData: function(data, DomainService) {
return $http.post(DomainService.host + 'factor', data);
}
};
}]);
app.factory('DomainService', function() { //here
if (ionic.Platform.isAndroid()) {
return {
host: 'http://10.0.2.2:7001/'
}
}
return {
host: 'http://localhost:7001/'
}
})
And my unit test skeleton is as follows. 我的单元测试骨架如下。 It has two dependencies so, couldn't figure out how to proceed. 它有两个依赖关系,因此无法弄清楚如何继续。 This is what I got so far for in unit test file. 这是我到目前为止在单元测试文件中得到的。
describe(
'datastoreServices',
function() {
beforeEach(module('Myapp'));
describe('getData'),
function() {
it("Should return correct values", inject(function(datastoreServices, DomainService, $httpBackend) {
expect(datastoreServices.getData(httpBackend.. /***something here!**/ )
.toEqual("2.2");
}))
}
I have very little knowledge on mocking and stuffs. 我对嘲笑和东西知之甚少。 Can someone help me test that factory datastoreServices
. 有人可以帮我测试工厂datastoreServices
。 The following things are to be tested: 以下内容需要测试:
Here is the similar scenario of app in plnkr . 这是plnkr中app的类似场景。
Idk, if I am asking too much. Idk,如果我问得太多了。 Thanks in advance. 提前致谢。
The key principles are: 关键原则是:
Here's an example based on your OP code: 以下是基于OP代码的示例:
describe('datastoreServices', function() {
beforeEach(module('MyApp'));
// get a reference to the $httpBackend mock and to the service to test, and create a mock for DomainService
var $httpBackend, datastoreServices, DomainService;
beforeEach(inject(function(_$httpBackend_, _datastoreServices_) {
$httpBackend = _$httpBackend_;
datastoreServices = _datastoreServices_;
DomainService = function() {
return {
host: 'http://localhost:7001/'
};
};
}));
// after each test, this ensure that every expected http calls have been realized and only them
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('calls http backend to get data', function() {
var data = {foo: 'bar'};
// write $http expectation and specify a mocked server response for the request
// see https://docs.angularjs.org/api/ngMock/service/$httpBackend
$httpBackend.expectPOST('http://localhost:7001/factor', data).respond(201, {bar: 'foo'});
var returnedData;
datastoreServices.getData(data, DomainService).success(function(result) {
// check that returned result contains
returnedData = result;
expect(returnedData).toEqual({bar: 'foo'});
});
// simulate server response
$httpBackend.flush();
// check that success handler has been called
expect(returnedData).toBeDefined();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.