Javascript AngularJS-修改的对象不会从一个测试进行到下一个测试

Javascript AngularJS-修改的对象不会从一个测试进行到下一个测试,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,因此,我在AngularJS应用程序的服务中有一个简单的对象: this.testObj = [ {'id': 0, 'label': 'Hello'} ]; 从.service('MainSvc',function(){ 在控制器中,我有: .controller('MainCtrl', function MainCtrl($scope, MainSvc) { this.testObj = MainSvc.testObj; }); 问题是,我正在做因

因此,我在AngularJS应用程序的
服务中有一个简单的对象:

    this.testObj = [
        {'id': 0, 'label': 'Hello'}
    ];
.service('MainSvc',function(){

在控制器中,我有:

.controller('MainCtrl', function MainCtrl($scope, MainSvc) {
    this.testObj = MainSvc.testObj;
});
问题是,我正在做因果报应/茉莉花测试,在第二次测试之前一切都很顺利……它不会识别出物体已经改变并得到新的

这是一个模拟的单元测试

describe('Mocked Unit Test', function() {
    'use strict';

    beforeEach(module('ui.router'));
    beforeEach(module('main'));

    var MainSvc, scope, http, mockMain;

    beforeEach(angular.mock.inject(function($rootScope, MainSvc, $controller, $injector){
        scope = $rootScope.$new();
        mockMain = $injector.get('MainSvc');

        $controller('MainCtrl', {
            $scope: scope,
            MainSvc: mockMain
        });

        scope.$digest();
    }));

    it('should expect item to be defined', inject(function() {
        expect(mockMain.testObj).toBeDefined(); // passes
        mockMain.testObj[1] = {id: 2, label: "World"};
        console.log(mockMain.testObj); // [Object{id: 0, label: 'Hello'}, Object{id: 2, label: 'World'}]
        console.log('Length: ' + Object.keys(mockMain.testObj).length); // Length: 2
    }));

    it('expect the testObj object to have a 2nd index', inject(function() {
        console.log(mockMain.testObj); // [Object{id: 0, label: 'Hello'}]
        console.log('Length: ' + Object.keys(mockMain.testObj).length); // Length: 1

        // Where is testObj[1] from when it was added in the first test?
    }));
});
如您所见,从内嵌注释中,第二个
it()
测试中的对象没有看到
mockMain.testObj
的对象已更改

或者也许我把它重新解释错了


请提供帮助。谢谢。

测试彼此不依赖,它们是孤立的。这是主要的单元测试原则之一。如果您希望两个测试之间有共同之处,请使用
beforeach()

或者您可以添加另一个
descripe
块,并在其中使用
beforeach()

beforeEach(angular.mock.inject(function($rootScope, MainSvc, $controller, $injector){
    scope = $rootScope.$new();
    mockMain = $injector.get('MainSvc');

    $controller('MainCtrl', {
        $scope: scope,
        MainSvc: mockMain
    });

    scope.$digest();
}));

it('should expect item to be defined', inject(function() {
    expect(mockMain.testObj).toBeDefined(); // passes
    mockMain.testObj[1] = {id: 2, label: "World"};
    console.log(mockMain.testObj); // [Object{id: 0, label: 'Hello'}, Object{id: 2, label: 'World'}]
    console.log('Length: ' + Object.keys(mockMain.testObj).length); // Length: 2
}));

describe('having two elements in the array', function() {
    beforeEach(function() {
        mockMain.testObj[1] = {id: 2, label: "World"};
    });

    it('expect the testObj object to have a 2nd index', inject(function() {
        console.log(mockMain.testObj); 
        console.log('Length: ' + Object.keys(mockMain.testObj).length); 
    }));

});

此外,对测试中定义的内容进行断言似乎是错误的。通过在数组中设置第二个元素,然后测试其长度,您只是在验证本机javascript分配。

测试彼此不依赖,它们是隔离的。这是主要的单元测试原则之一。如果您想拥有一些g在两个测试之间,您应该使用
beforeach()

或者您可以添加另一个
descripe
块,并在其中使用
beforeach()

beforeEach(angular.mock.inject(function($rootScope, MainSvc, $controller, $injector){
    scope = $rootScope.$new();
    mockMain = $injector.get('MainSvc');

    $controller('MainCtrl', {
        $scope: scope,
        MainSvc: mockMain
    });

    scope.$digest();
}));

it('should expect item to be defined', inject(function() {
    expect(mockMain.testObj).toBeDefined(); // passes
    mockMain.testObj[1] = {id: 2, label: "World"};
    console.log(mockMain.testObj); // [Object{id: 0, label: 'Hello'}, Object{id: 2, label: 'World'}]
    console.log('Length: ' + Object.keys(mockMain.testObj).length); // Length: 2
}));

describe('having two elements in the array', function() {
    beforeEach(function() {
        mockMain.testObj[1] = {id: 2, label: "World"};
    });

    it('expect the testObj object to have a 2nd index', inject(function() {
        console.log(mockMain.testObj); 
        console.log('Length: ' + Object.keys(mockMain.testObj).length); 
    }));

});
此外,对测试中定义的内容进行断言似乎是错误的。通过在数组中设置第二个元素,然后测试其长度,您只是在验证本机javascript分配。

我相信每个元素都是它()它有自己的作用域和模拟对象,因此从一个测试到另一个测试的更改不会进行,因为这将挑战单元测试的统一性。我相信每个it()都有自己的作用域和模拟对象,因此从一个测试到另一个测试的更改不会进行,因为这将挑战单元测试的统一性。