如何在业力中更改范围变量

时间:2017-02-01 09:01:02

标签: angularjs karma-runner karma-jasmine

我有一个测试用例scope.action = 'Insert'需要为每个它更改(),对于第一个测试用例,它应该是insert用于第二个测试用例scope.action='update'和第三个测试用例测试用例应该是scope.action='delete'。我怎样才能做到这一点。

'use strict';

describe('app module',function(){

beforeEach(module('sampleApp'));

beforeEach(module(function ($provide) {
  $provide.value('BaseController', {});
}));

describe('TestController', function() {
  var scope, controller;
      beforeEach(inject(function($rootScope, $controller) {
          scope = $rootScope.$new();
          controller = $controller;
          controller('BaseController', {$scope: scope});
          controller('TestController', {
          $scope: scope
      });
         scope.action="insert" ;
  }));
  it('Should return type insert', function () {
      expect(scope.getActionType()).toBe('insert');
  });
  it('Should return type update', function () {
      expect(scope.getActionType()).toBe('update');
  });
  it('Should return type delete', function () {
      expect(scope.getActionType()).toBe('delete');
  });

}); });

1 个答案:

答案 0 :(得分:0)

你可以通过描述中的描述来实现它。外部的beforeEach()块设置了东西,但内部的东西设置了你的范围:

describe('Test Controller', () => {
  beforeEach(() => {
    // In here, do what you currently have in your beforeEach
    // but NOT the scope.action; EXCEPT the controller;
  });

  describe('Test 1', () => {
    beforeEach(() => {
      scope.action = 'Insert'
      controller('TestController', { $scope: scope });
    });
    it('First Test', () => {});
  });

  describe('Test 2', () => {
    beforeEach(() => {
      scope.action = 'Update';
      controller('TestController', { $scope: scope });
    });
    it('Second Test', () => {});
  });

  describe('Test 3', () => {
    beforeEach(() => {
      scope.action = 'Delete';
      controller('TestController', { $scope: scope });
    });
    it('Third Test', () => {});
  });
});