Я создал директиву «generate-form-field-directive», которая создаст поле формы на основе полученного типа. Ниже приведен код для директивы -Проверьте наличие элемента html в директиве во время модульного тестирования
(function() {
"use strict";
var app = angular.module("appModule");
app.directive("generateFormField", generateFormField);
/*@ngInject*/
function generateFormField() {
return {
restrict: "E",
scope: {
attributeDetails: "=",
fieldName: "="
},
replace: false,
controller: function($scope, actionStore) {
var vm = this;
},
template: "<div class='col-sm-9 generate-form-field-container'>" +
"<input type='text' class='form-control' name='{{fieldName}}' ng-if='attributeDetails.type === \"String\"' ng-model='attributeDetails.value' required>" +
"<input type='number' class='form-control' name='{{fieldName}}'' ng-if='attributeDetails.type === \"Integer\"' ng-model='attributeDetails.value' required>" +
"</div>"
};
}
}());
Так что если attributeDetails.type является «String», то первый вход тег будет оказан. То же самое для attributeDetails.type = integer. Теперь я хочу проверить, присутствует ли «вход [тип = текст]» в DOM или нет, когда я передаю атрибут obkecj: { «type»: «String», «name»: «Name», « значение ":" Volume1 " }.
тестовый файл блок -
describe("UNIT DIRECTIVE: generateFormField", function() {
"use strict";
// Angular injectables
var $compile, $rootScope, $httpBackend, $q;
// Module defined (non-Angular) injectables
var $scope, generateFormField, actionStore;
// Local variables used for testing
var element, formRequestResponse, directiveScope, vm, template, getListOptions;
// Initialize modules
beforeEach(function() {
module("appModule");
});
beforeEach(function() {
inject(function (_$compile_, _$rootScope_, _$httpBackend_, _$q_, _actionStore_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
$q = _$q_;
$scope = $rootScope.$new();
});
});
describe("generateFormField unit testing", function() {
beforeEach(function() {
template = "<generate-form-field attribute-details='attribute' field-name='key'></generate-form-field>";
$scope.attribute = {
"type":"String",
"name": "Name",
"value": "Volume1"
},
$scope.key = "volName";
element = $compile(template)($scope);
directiveScope = element.find("generate-form");
// Exercise SUT
$scope.$digest();
});
**//how to check for input[type=text]**
it("it should create input button of type='text'", function() {
//expect(element.html()).indexOf("type='text'").to.not.equal(-1);
//expect(element("input[type=text]").length).to.be(1);
});
});
});
Это будет хорошо, если вы можете предоставить работать скрипку для него. Заранее спасибо.
сделал [мой ответ] (http://stackoverflow.com/a/40374135/2545680) Помогите? –
@Maximus Спасибо :) –
добро пожаловать, вы можете принять его, если он решил вашу проблему) –