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