Scope variable how to access in jasmine framework using angularjs - angularjs

I injected controller all dependencies into spec.js file and trying to access the scope variable but am getting TypeError: Cannot read property 'itemsByPage' of undefined spec.js file mocked below;
`describe('baseController', function() {
beforeEach(module('seApp'));
var $controller,$scope,$compile, $timeout, $mdSidenav, $log,
$http,$uibModal, $routeParams, $window,$mdDialog;
inject(function($controller,$rootScope,$compile, $timeout,$mdSidenav,
$log, $http,$uibModal, $routeParams, $window,$mdDialog){
$scope = $rootScope.$new();
$compile = $compile,
$timeout =$timeout,
$mdSidenav =$mdSidenav,
$log = $log,
$http = $http,
$routeParams = $routeParams,
$window = $window,
$mdDialog =$mdDialog,
$uibModal = $uibModal;
baseController = $controller('baseController', {
$scope : $scope,
$compile : $compile,
$timeout :$timeout,
$mdSidenav :$mdSidenav,
$log : $log,
$http : $http,
$routeParams : $routeParams,
$window : $window,
$mdDialog : $mdDialog,
$uibModal : $uibModal
});
});
it('check the base controller', function() {
expect(1).toBe(1);
});
describe('check scope', function() {
it('check itemsBy page', function() {
expect($scope.itemsByPage).toEqual('8');
}); });});`
execution report is Executed 2 of 2 (1 FAILED) controller is passing but not able to access scope variable inside the controller. please suggest me am new for jasmine framework

Related

controller is not registered error while running testcase via Jasmine Karma

I am trying to write test case for angularjs controller via karma jasmine
Getting below error
Error: [$controller:ctrlreg] The controller with the name 'ApproveRejectPackageController' is not registered.
This is my controller code and supporting test case
(function() {
angular.module('Citi.iv.FC')
.controller('ApproveRejectPackageController', ['$scope', '$filter', '$timeout','$uibModal','$sce', '$stateParams', function ($scope,$filter, $timeout, $uibModal, $sce, $stateParams) {});
describe('myserv', function () {
var service;
var data;
beforeAll(function () {
module('Citi.iv.FC')
});
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope;
secondController = $controller('ApproveRejectPackageController', {$scope: scope});
}));
it('ServiceTestSpec', function () {
expect(2 + 3).toBe(6);
});
});
Please suggest how to fix this.
I have included controller in karma-config file

Jasmine Error when trying to set up controller object

This is the actual error message that I receive:
Uncaught Error: [$injector:unpr] Unknown provider: formsProvider <- forms <- SignupAcclaimController
this is at the start of my test file:
describe("SignUp", function() {
describe("SignupAcclaimController", function () {
beforeEach(module('CL.SignUp'));
var controller, SignupService, $state, scope, identity;
beforeEach(inject(function (_$controller_, _$state_, _SignupService_, _$rootScope_, _identity_) {
$state = _$state_;
scope = _$rootScope_.$new();
SignupService = _SignupService_;
identity = _identity_;
controller = _$controller_('SignupAcclaimController', {
$state: $state,
SignupService: SignupService,
identity: _identity_,
detailsForm: mocks.form
});
}));........
The error occurs when trying to set controller. mocks.form has been declared above this code.
The actual controller is:
(function() {
'use strict';
angular.module('CL.SignUp')
.controller('SignupAcclaimController', SignupAcclaimController);
SignupAcclaimController.$inject = ['$window', '$state', '$log', 'SignupService', 'identity', 'detailsForm', 'forms', '$rootScope', '$scope', '$timeout'];
function SignupAcclaimController($window, $state, $log, SignupService, identity, detailsForm, forms, $rootScope, $scope, $timeout) {
var vm = this, ........
If I change the injector functions signature to be the same as the method signature of the actual controller then expand the controller = to be
controller = _$controller_('SignupAcclaimController', {
$window: _$window_,
$state: $state,
$log: $log,
SignupService: SignupService,
identity: _identity_,
detailsForm: mocks.form,
forms: _forms_,
$scope: scope,
$rootScope: _$rootScope_,
$timeout: _$timeout_
then the error changes to :
Uncaught Error: [$injector:unpr] Unknown provider: formsProvider <- forms

Error in Jasmine Test in Angular Js

i want to test functions in angular js using jasmine. i have created one controller in which one scope function which is given below.
app.controller('NavigationCtrl', ['$scope', '$route', '$rootScope', function ($scope, $route, $rootScope) {
$scope.title="Hello World";
$scope.testMe = function () {
$scope.title = "Welcome";
}
}]);
Also, i want to call above controller function i.e $scope.testMe from another controller using jasmine test case. so, i create another controller and try to call functions from this controller.
app.controller('RunTestCaseCtrl', ['$scope', '$http', '$location', '$route', '$routeParams', '$rootScope', '$timeout', 'MDT_Constant', function ($scope, $http, $location, $route, $routeParams, $rootScope, $timeout, MDT_Constant) {
describe('mycontroller', function () {
beforeEach(module('app'));
it('scopeTestSpec',
inject(function ($controller, $rootScope) {
var $scope = $rootScope.$new();
$controller('NavigationCtrl', {
$scope: $scope
});
$controller.testMe();
expect($scope.title).toEqual('Welcome');
console.log('ok');
}));
});
}]);
this will give the error "module is not defined".
so, how can i call controller method from another controller using jasmine test

Mocking $location service in angular unit test

I am new to developing in angular, and am trying to learn how to test angular controllers. The controller I am testing uses $location.seach().something. I looked at the docs for $location, but don't quickly see how I am supposed to mock this in karma/jasmine.
The controller:
rmtg.controller('ErrorCtrl', ['Session', '$location', '$routeParams', '$scope', '$window',
function(Session, $location, $routeParams, $scope, $window) {
console.log('ErrorCtrl(%o, %o, %o)', $location.path(), $location.search(), $routeParams);
$scope.status = $location.search().status;
$scope.message = $location.search().message;
$scope.isAuthorized = (typeof(Session.auth) === 'object');
$scope.signin = function() {
$window.location = '/signin/#/' + $routeParams.origin + (Session.auth ? '?email=' + Session.auth.email : '');
};
}]);
My current spec attempt:
'user strict';
describe('Testing the errorCtrl controller', function(){
beforeEach(module("rmtg"));
var errorCtrl, scope;
beforeEach(inject(function($controller, $rootScope){
scope = $rootScope;
errorCtrl = $controller("ErrorCtrl", {
$scope: scope
});
}));
it('$scope.status should be set to 404 when location is set to 404', function(){
//set the $location.search values so that the scope is correct
$location.search('status', '404');
expect(scope.status).toBe('404');
});
});
And the current error message:
Testing the errorCtrl controller $scope.status should be set to 404 when location is set to 404 FAILED
Expected undefined to be '404'.
at Object. (/Users/adamremeeting/git/mrp-www/app/tests/example.js:20:24)
I'd also really appreciate links to resources on tdd with angular 1.5 and how I mock and stub correctly.
Edit After Answer
So I updated the test as per user2341963 suggestions, and did my best to look through his plunker example, but still don't have a passing test.
the current spec (controller has not changed from above)
'user strict';
describe('ErrorCtrl', function(){
beforeEach(module("rmtg"));
var scope, $location, $controller;
beforeEach(inject(function(_$controller_, _$rootScope_, _$location_){
scope = _$rootScope_.$new();
$location = _$location_
$controller = $_controller_;
}));
describe('$scope.status', function(){
it('should set status to 404', function(){
//set the $location.search values so that the scope is correct
$location.search('status', '404');
//init controller
$controller('ErrorCtrl', {
$scope: scope,
$location: $location
});
expect(scope.status).toBe('404');
});
});
});
But I am getting an error now that $controller is not defined.
You are getting undefined in your test because you are not setting $location anywhere.
Based on your controller, the search parameters must be set before the controller is initialised. See plunker for full example.
describe('testApp', function() {
describe('MainCtrl', function() {
var scope, $location, $controller;
beforeEach(module('myApp'));
beforeEach(inject(function(_$rootScope_, _$controller_, _$location_) {
scope = _$rootScope_.$new();
$location = _$location_;
$controller = _$controller_;
}));
it('should set status to 404', function() {
// Set the status first ...
$location.search('status', '404');
// Then initialise the controller
$controller('MainCtrl', {
$scope: scope,
$location: $location
});
expect(scope.status).toBe('404');
});
});
});
As for resources, so far I've found the angular docs are good enough.

whenDELETE : undefined is not a function

Here is what I am doing :
controller.js
var app = angular.module('app', [ 'angularFileUpload' ]);
app.controller('MyCtrl', [ '$scope', '$http', '$timeout', '$upload',
function($scope, $http, $timeout, $upload) {
$scope.something;
$scope.deleteFile = function(fileId) {
$http({
method : 'DELETE',
url : "http://xxx.xxx.xx.xx:8080/fileId
}).success(function(response) {
scope.something=response;
console.log(response);
});
});
controllerspec.js
ddescribe("MyCtrl test cases ",function(){
var scope , controller ,httpBackend;
beforeEach(angular.mock.module('app'));
beforeEach(angular.mock.inject(function($rootScope,$controller,$http, $timeout, $upload){
scope = $rootScope.$new();
httpBackend = $http;
$controller('MyCtrl',{
$scope:scope,
$http:httpBackend,
$timeout:$timeout,
$upload:$upload
});
}));
it("should delete selected file ",function(){
/*HERE*/ httpBackend.when('DELETE','http://xxx.xxx.xx.xx:8080/1').respond
({
"deleted"
});
httpBackend.flush();
scope.deleteFile(1);
expect(scope.something).toBeDefined();
});
I get TypeError: Undefined is not a function at line "HERE".
What am I missing ?
You have error in your code because of following piece of code
Instead of
beforeEach(angular.mock.inject(function($rootScope,$controller,$http, $timeout, $upload){
scope = $rootScope.$new();
httpBackend = $http;
$controller('MyCtrl',{
$scope:scope,
$http:httpBackend,
$timeout:$timeout,
$upload:$upload
});
use
beforeEach(angular.mock.inject(function($rootScope,$controller,$httpBackend, $timeout, $upload){
scope = $rootScope.$new();
httpBackend = $httpBackend;
$controller('MyCtrl',{
$scope:scope,
$timeout:$timeout,
$upload:$upload
});
$httpBackend is used for mocking http requests, no need to inject it inside controller, instead you may inject $http service. Also you had error as you didn't injected httpBackend service and were trying to use its function that why you got undefined error.

Resources