我有2个数据数组:
array_1=['temp1','temp2','temp3'];
array_2=['insert','delete','update']
我有测试用例传递两个值,如
it('Should return type insert', function () {
expect(scope.getActionType(array_1[some incrementer variable])).toBe(array_2[some incrementer variable]);
});
但我需要通过使用一个循环来实现这一点,并且只使用一个循环,你可以帮助我。
'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');
});
});
});
答案 0 :(得分:0)
我假设当您说不想使用循环时,您不想在测试内部运行循环,从而使单个测试运行多个执行。
您可以做的是将循环放在测试之外,如下所示:
for (var [value, expected] of [
['temp1', 'insert'],
['temp2', 'delete'],
['temp3', 'update']
]) {
test_getActionType(value, expected);
}
function test_getActionType(value, expected) {
it('Should return expected type (' + value + ' -> ' + expected + ')', function() {
var result = scope.getActionType(value);
expect(result).ToBe(expected);
});
}
必须使用单独的函数,以便实际测试中的匿名函数关闭value
和expected
变量的正确值。