jasmine

How do I Mock RouterStateSnapshot for a Router Guard Jasmine test

倖福魔咒の 提交于 2019-12-03 15:30:19
问题 I have a simple router guard and I am trying to test the canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ) . I can create the ActivatedRouteSnapshot like this new ActivatedRouteSnapshot() but I cannot figure out how to create a mocked RouterStateSnapshot . Per the code I tried... let createEmptyStateSnapshot = function( urlTree: UrlTree, rootComponent: Type<any>){ const emptyParams = {}; const emptyData = {}; const emptyQueryParams = {}; const fragment = ''; const

Javascript JsTestDriver Jasmine & Jasmine-jquery

笑着哭i 提交于 2019-12-03 14:48:59
问题 I have lots of Jasmine unit tests, that are running unit tests for Javascripts code. They are using Jasmine-jquery plugin to do DOM manipulation. they use loadFixture, to load fixtures of HTML I tried to automate those unit tests, using JsTestDriver, with JasmineAdapter But all tests involve DOM-jquery manipulation are not passing? Is there something wrong with that? Is there a way to use Jasmine-jquery with JsTestDriver? 回答1: I will answer myself because I found a solution for this problem.

Protractor, Jasmine, and stopping test on first fail

十年热恋 提交于 2019-12-03 14:47:28
While trying to figure out how to make certain jasmine expect statements dependent on a previous expect statement I discovered that previous to Jasmine 2.3.0, there was not a way. (see Stop jasmine test after first expect fails ) However, Jasmine 2.3.0 added an option stopSpecOnExpectationFailure that when set to true will stop a test on the first failure. Excited by this prospect, I modified my conf.js to include the option: /* * conf.js */ exports.config = { framework: 'jasmine', specs: ['search-spec.js'], useAllAngular2AppRoots: true, jasmineNodeOpts: { stopSpecOnExpectationFailure: true }

Test if a promise is resolved or rejected with Jasmine in Nodejs

蓝咒 提交于 2019-12-03 14:41:10
问题 I know how to do it in Mocha but want to know how to do it with Jasmine. I tried this describe('test promise with jasmine', function() { it('expects a rejected promise', function() { var promise = getRejectedPromise(); // return expect(promise).toBe('rejected'); return expect(promise.inspect().state).toBe('rejected'); }); }); However, the state is always pending and, of course, the test fails. I couldn't find any example online that I could make it work. Can someone please help me with this?

AngularJS $timeout function not executing in my Jasmine specs

泪湿孤枕 提交于 2019-12-03 14:25:38
问题 I'm trying to test my AngularJS controller with Jasmine, using Karma. But a $timeout which works well in real-life, crashes my tests. Controller: var Ctrl = function($scope, $timeout) { $scope.doStuff = function() { $timeout(function() { $scope.stuffDone = true; }, 250); }; }; Jasmine it block (where $scope and controller have been properly initialized): it('should do stuff', function() { runs(function() { $scope.doStuff(); }); waitsFor(function() { return $scope.stuffDone; }, 'Stuff should

How to unit test async Redux actions to mock ajax response

我们两清 提交于 2019-12-03 13:52:14
I am creating a middleware for making ajax requests using async actions. The middleware intercepts original action, performs ajax request, and re- dispatch es the original action along with the response from the url . So, my Component would merely dispatch an action like this onClick() { dispatch(ActionCreator.fetchUser()); } Rest will be taken care by the middleware as shown here . My question is, what should I do for unit testing? Should I mock the onClick itself? or I should write a mocked middleware and forward the actions with the mocked response? I am not sure which approach should I

Angular.js unitest a Directive with external template using html2js - Fail to load templates

﹥>﹥吖頭↗ 提交于 2019-12-03 13:35:53
I am trying to test a Directive which uses external template. I tried all the following solutions with no luck: ng-directive-testing How to test directives that use templateUrl and controllers? AngularJS + Karma + Ng-html2js => Failed to instantiate module ...html I created a test directive (a simple div) and tested it using an inline 'template' and external 'templateUrl'. The inline solution works while the external doesn't: angular.module('AdUnit').directive('actionButton',function($location){ return{ scope:{ actionName: '@' }, restrict: 'E', //template: "<div ng-click='click()'>action

.detectChanges() not working within Angular test

我只是一个虾纸丫 提交于 2019-12-03 13:34:10
I've been tasked with writing tests for a chat app developed with Angular. Below is the snippet of Angular template code I'm currently writing tests for: <div class="title-menu-container" fxLayoutAlign="center center"> <button id="save-title-button" mat-icon-button *ngIf="titleInputEdit; else settings"> <mat-icon class="secondary-text" (click)="saveTitle(titleInput.value)">check</mat-icon> </button> <ng-template #settings> <button mat-icon-button [matMenuTriggerFor]="menu" [disabled]="!(isGroupConversation$ | async)"> <mat-icon class="secondary-text">settings</mat-icon> </button> </ng-template

karma.conf.js automatic file ordering?

旧巷老猫 提交于 2019-12-03 13:33:30
I have a large angularjs project ordered by features. I'd like to setup unit testing but I'm having trouble getting the karma.conf.js file ordering setup. I tried specifying a simple glob pattern like **/*.js but many of my modules failed to load due to the ordering that they're included in Karma when ran. As I understand, it's alphabetical, first match. I was able to resolve this by manually figuring out the ordering by doing something like this: // list of files / patterns to load in the browser files: [ // External scripts 'bower_components/angular/angular.js', 'bower_components/angular

Error: Cannot find module 'jasmine-core'

对着背影说爱祢 提交于 2019-12-03 13:15:23
问题 I installed the following for testing: "devDependencies": { "jasmine-core": "^2.4.1", "karma": "^0.13.22", "karma-jasmine": "^0.3.7", "karma-phantomjs-launcher": "^1.0.0" } After running karma start I get the following error: Doing a search this is the first question with the same problem: karma start Cannot find module 'jasmine-core' However I've tried both answers, installed jasmine-core globally and I already did npm install jasmine-core --save-dev :( My test/index.html <!DOCTYPE html>