Search Posts

Category: jasmine

problems while doing unit testing with controller in angularjs

All the karma configuration are right and it runs fine with other controller files.But it could not run this controller file. I will be thankful if you guys find any solutions for this. 🙂 the screenshot of the source code, testing, and commond prompt are as follows 1. controller file 2. jasmine unit testing file 3. after running karma, error displays command prompt Source: AngularJS

Unit testing that a component is rendered in an angular app

I have an Angualr 1.5 app that looks like this. The app has a mix of Typescript and Javascript components: import * as angular from ‘angular’; import {B} from ‘./b.component’; import {A} from ‘./a.module’; export const c= ‘c’; angular.module(c, [a]) .component(B.componentId, new B()); The corresponding HTML looks like this <div> <div id=”holder”> <a></a> </div> </div> And here is the b.module angular.module(a, []) .component(b.componentId, new A()); And here is the a.component import * as ng from […]

Jasmine TypeScript unit test fails if types in methods being tested are not fully qualified

So I have successfully got unit testing working with my angularjs app using Jasmine and Karma. However, I’ve just hit an annoying issue I can’t figure out. So I have a controller with a very simple method like this (this is a random example so don’t worry if it seems weird): public ExecuteUserCreatedCommand( id: number, userId: string) { const command = new Commands.UserCreated(); command.Id = id; command.userId = userId; return this.CommandService.UserCreated(command); } Note that the […]

Mocking Angular service in Jasmine throws method not found

In this plunk I have an Angular/Jasmine test that attempts to mock a service function. The service function mocked needs to be invoked by another function in the service. This is the error I get: Error: getTheDate() method does not exist And this is my attempt, where the function tested getTheMonth should invoke a mocked function getTheDate, it seems that spyOn is used incorrectly: angular.module(“mymodule”, []) .service(‘myService’, function(){ this.getTheDate = function() { return new Date(); […]

Test angularjs with Jasmine using stand alone command line interface

I have a simple .js file and its test on Jasmine. I’m able to run the test successfully through Jasmine’s stand alone SpecRunner.html. Is it possible to do the same on command line interface? I do have node installed along with jasmine, npm install -g jasmine Tests using Jasmine Command line interface works for simple javascript functions that return simple objects. However if I start using angular libraries, it results in: })(window, document); ^ ReferenceError: […]

How to fix the error ‘Module is not available! You either misspelled the …’

I’m only using Angularjs and Jasmine. I got an error Module is not available! You either misspelled the … and i’m looking to fix it without success. my-template.html <html ng-app=”myApplication”> <head> <script src=”lib/scripts/1_4_4/angular.min.js”></script> </head> <body> <div ng-controller=”SimpleController”> {{amount.length}} </div> <script> var myModule = angular.module(‘myApplication’, []); myModule.controller(‘SimpleController’, function($scope) { $scope.amount = [‘a’,’b’,’c’]; }); </script> </body> </html> my-template.spec.js describe(‘a simple controller’, function(){ var $scope; //See API angular.mock.module beforeEach(module(‘myApplication’)); beforeEach( inject( function( $rootScope, $controller ){ $scope = $rootScope.$new(); […]

How to mock chained promises using Jasmine?

I am writing a unit test to a method that contains this piece of code: Name.get($scope.nameId).then(function(name){ Return name; }).then(doSomething); The function doSomething(name) looks like this. function doSomething(name){ addNametoList(name); } I don’t need to test this part of the code. Since I can’t just ignore it in my test (or can I?), I need to mock it. I mocked the first promise spyOn(mockName, ‘get’).and.returnValues($q.resolve({“Mike”})); and thought that it would propagate through the second then(doSomething) but name […]

Jasmine / Karma test error using angular.mock.inject

I am trying to run some Angular karma/jasmine tests to test functionality for ui-grid. But my issue is that my tests are breaking when they get to the injector.Here is my error: C:/Users/schmitzb/Projects/RCO/rco-project/test/index.js:9:280261 [email protected]:/Users/schmitzb/Projects/RCO/rco-project/test/index.js:9:11940 [email protected]:/Users/schmitzb/Projects/RCO/rco-project/test/index.js:9:277471 c[email protected]:/Users/schmitzb/Projects/RCO/rco-project/test/index.js:9:139593 [email protected]:/Users/schmitzb/Projects/RCO/rco-project/test/index.js:9:1071148 [email protected]:/Users/schmitzb/Projects/RCO/rco-project/test/index.js:9:1069633 C:/Users/schmitzb/Projects/RCO/rco-project/test/index.js:9:1007389 TypeError: undefined is not a constructor (evaluating ‘$compile(‘<bss></bss>’)’) in C:/Users/schmitzb/Projects/RCO/rco-project/test/index.js (line 9) C:/Users/schmitzb/Projects/RCO/rco-project/test/index.js:9:1008235 I’m doing something incorrectly at this line I believe, when I remove this line and put in a different module it doesn’t error: […]

Testing Conditional Data Binding in Angular

I have the following code: <div class=”myClass”> {{ myFunction(a) ? person.name : “Name not given” }} </div> I want to be able to unit test this specific element in my HTML so that when the function myFunction(a) fails, I can ensure that the text is Name not given. Any ideas on how I can accomplish this using Jasmine? Thanks Source: AngularJS

Jasmine – how to loop a describe block – dynamic describes

I need to run a test which loads some JSON files and builds a set of inputs. I would like to create just one test which would run the same test for each file. Currently the tests in Jasmine fail without any reason given; they just exit. // each page contains a list of inputs describe(‘test a few files’, function () { var $rootScope; var $compile; var $scope; var element; beforeEach(function () { module(‘myModule’); inject(function […]

Next Page »