I am new to unit testing ,i need to write unit test scenario for creating task and getting task by giving certain number
This is my routes
state('faqs.summary', {
url: '/summary',
views: {
'main-display': {
templateUrl: 'views/summary.html',
controller: 'summaryFaqsCtrl'
}
}
}).
state('faqs.summary.create', {
url: '/create/:id',
views: {
'child-display': {
templateUrl: 'views/itemform.html',
controller: 'createFaqCtrl'
}
}
})
my controllers code is as follows
var app = angular.module('faqAdminResourceApp');
app.controller('createFaqCtrl', function ($scope, $state, FaqService, $stateParams, toasty, usSpinnerService) {
$scope.createFaqItem = function () {
console.log('Checking form value : ' + JSON.stringify($scope.faqSingle));
var faq = new FaqService({
'subject': this.faqSingle.subject,
'text': this.faqSingle.text,
'category': this.faqSingle.category,
'internal': this.faqSingle.internal
});
faq.$save(function (response) {
if (response.ErrMessage) {
$scope.error = response.ErrMessage;
toasty.pop.warning({
title: 'Subject already Exists !',
sound: false,
showClose: true,
clickToClose: false,
timeout: 3000
});
} else {
toasty.pop.success({
title: 'Successfully added FAQ !',
sound: false,
showClose: true,
clickToClose: false,
timeout: 3000
});
$scope.faqSingle = '';
$scope.newFaq = false;
$state.go('faqs.summary');
}
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
}
});
app.controller('summaryFaqsCtrl', function ($scope, $state, FaqService, faqSummaryService, $rootScope,usSpinnerService) {
$scope.getSummaryFaqs = function (summaryFaqDepth) {
if ($state.current.name==='faqs.summary') {
usSpinnerService.spin('spinner-1');
}
faqSummaryService.query({
summaryDepth: summaryFaqDepth
}, function (res) {
$scope.faqSumItems = res;
if ($state.current.name==='faqs.summary') {
usSpinnerService.stop('spinner-1');
}
});
};
});
And this is my services code
app.factory('FaqService', function ($resource, ConfigService) {
// AngularJS will instantiate a singleton by calling "new" on this function
return $resource(ConfigService.API_URL + 'faqs/:faqId', {
faqId: '@faqId'
}, {
'get': {
method: 'GET'
},
'save': {
method: 'POST'
},
'update': {
method: 'PUT'
},
'query': {
method: 'GET',
isArray: true
},
'remove': {
method: 'DELETE'
},
'delete': {
method: 'DELETE'
}
});
});
app.factory('faqSummaryService', function ($resource, ConfigService) {
return $resource(ConfigService.API_URL + 'faqs/summary/:summaryDepth', {
summaryDepth: '@summaryDepth'
}, {
'query': {
method: 'GET',
isArray: true
}
});
});
Here i tried to write unit test for create task.while exceuting the task it shows an error that scope.faqSingle.length is not defined.with out checking the length How i no wheather the task is creating are not. This is my following test code
test.js
describe('Controller: createFaqCtrl', function () {
// load the controller's module
beforeEach(module('faqAdminResourceApp'));
beforeEach(function() {
jasmine.addMatchers({
toEqualData: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
return {
pass: angular.equals(actual, expected)
};
}
};
}
});
});
var createfaqCtrl,
scope,$httpBackend;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope,_$httpBackend_) {
scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
createfaqCtrl = $controller('createFaqCtrl', {
$scope: scope
});
}));
it('Create a Faq item', inject(function(FaqService){
scope.faqSingle = new FaqService({
subject:'New Item',
text:'A new item is created in db',
category:'general',
internal:true
});
var faqSingle=scope.faqSingle; $httpBackend.whenPOST('http://localhost:5000/api/v1/faqs',scope.faqSingle).respond(200);
scope.createFaqItem();
expect(scope.createFaqItem).toBeTruthy();
expect(scope.faqSingle.length).toBe(1);
}));
});
Aucun commentaire:
Enregistrer un commentaire