0
我有几个茉莉花测试套件,其中一些使用我实施的服务。 Jasmine中的服务行为是什么?我注意到当我对我的服务进行一些测试时,我正在做的事会影响到其他unitTests。我们知道这不可能发生,因为unitTests必须完全独立。即茉莉花服务行为
it('Given my service with list populated when cleanList then getList return empty list', function(){
myService.populateList(_myData_);
expect(myService.getList().length).toBe(5);
myService.cleanList();
expect(myService.getList().length).toBe(0);
expect(myService.getList()).toEqual([]);
});
如果我执行之前在其它另一个测试的测试中描述另一个完全不同的JS脚本文件我其他的测试开始,因为我希望使用该列表填充但未能做到这一点已经洗干净。在我的应用程序中,这里的服务是单身吗?我可以为每个描述套件创建这些服务的独立实例吗?
这就是我使用这些服务的方式。
(function(){
'use strict';
var _data_ = require('../../../../json/dataMock.json');
fdescribe('MyApp wizard:', function(){
var controller, createController;
var modalInstance = { close: function(){}, dismiss: function(){} };
var myService;
beforeEach(function(){
angular.mock.module('myapp.ui.apps.wizard');
angular.mock.module('myapp.ui.apps.wizard.service');
});
beforeEach(inject(function($injector, $controller, $rootScope, _$sce_, _myService_){
myService = _myService_;
createController = function() {
return $controller('MaintenancesWizardController', {
$rootScope: $rootScope,
$uibModalInstance: modalInstance,
data: _data_,
$sce: _$sce_,
myService: _myService_
});
}
}));
it('this test fails if the one before is executed', function(){
controller = createController();
expect(myService.getList().length).toBe(5);
});
你想测试myservice吗?您可以为您的服务使用间谍服务,以便它实际上不会调用该服务并更改该值。 –
@MukulJayaprakash我刚刚发现了发生了什么!阅读我的答案 – acostela