jasmine

How do I Mock RouterStateSnapshot for a Router Guard Jasmine test

天大地大妈咪最大 提交于 2019-12-03 05:08:28
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 activated = new ActivatedRouteSnapshot(); const state = new RouterStateSnapshot(new TreeNode

Conditionally ignore individual tests with Karma / Jasmine

こ雲淡風輕ζ 提交于 2019-12-03 04:45:12
I have some tests that fail in PhantomJS but not other browsers. I'd like these tests to be ignored when run with PhantomJS in my watch task (so new browser windows don't take focus and perf is a bit faster), but in my standard test task and my CI pipeline, I want all the tests to run in Chrome, Firefox, etc... I've considered a file-naming convention like foo.spec.dont-use-phantom.js and excluding those in my Karma config, but this means that I will have to separate out the individual tests that are failing into their own files, separating them from their logical describe blocks and having

What does the “it” function do in this code?

非 Y 不嫁゛ 提交于 2019-12-03 04:43:40
问题 I'm hoping somebody could explain to me what "it" does (is used for) in AngularJS or just plain JavaScript (I'm not sure if it's specific to Angular). This, turns out, is a difficult thing to Google for, being named "it" and all. I've seen it used throughout the AngularJS docs. I'll give you an example from the ngShow page (it's code to hide/show a div containing a thumbs up or thumbs down). var thumbsUp = element(by.css('span.glyphicon-thumbs-up')); var thumbsDown = element(by.css('span

Test a controller with success() and error ()

喜欢而已 提交于 2019-12-03 04:42:46
问题 I'm trying to work out the best way to unit test success and error callbacks in controllers. I am able to mock out service methods, as long as the controller only uses the default $q functions such as 'then' (see the example below). I'm having an issue when the controller responds to a 'success' or 'error' promise. (Sorry if my terminology is not correct). Here is an example controller \ service var myControllers = angular.module('myControllers'); myControllers.controller('SimpleController',

Mocking Controller Instantiation In Angular Directive Unit Test

痴心易碎 提交于 2019-12-03 04:40:48
问题 I am unit testing an Angular directive and would like to mock or stub in some way the instantiation of the named controller in the unit test. So first I suppose on to some code... 'use strict'; angular.module('App.Directives.BreadCrumbs', []) .directive('kxBreadcrumbs', function () { return { restrict: 'E', controller: 'BreadCrumbsController', template: '<!-- Breadcrumbs Directive HTML -->' + '<ol class="breadcrumb">' + ' <li ng-repeat="crumb in crumbPath">' + ' <a ng-class="{true: \'disable\

How to inject a service in a directive unit test in AngularJS

主宰稳场 提交于 2019-12-03 04:40:43
问题 I need to test a directive that does some calls to some injected services. The following piece of code is an example directive, that listens for events, and redirects the browser if enter is pressed inside a specified element. Edit: I get the feeling I may be wading in E2E testing land? angular.module('fooApp') .directive('gotoOnEnter', ['$location', function ($location) { var _linkFn = function link(scope, element, attrs) { element.off('keypress').on('keypress', function(e) { if(e.keyCode ==

Generate jasmine report using Karma Runner

天大地大妈咪最大 提交于 2019-12-03 04:32:34
I want to obtain a report of all successful jasmine specs runned with karma, something like you obtain when using jasmine alone. Is there anyway to achieve this? Try this quick plugin I wrote: https://github.com/dtabuenc/karma-html-reporter Tripp Lilley tl;dr Yes, but it's non-trivial, and even more so if you want --auto-watch . So, basically, no :-( Blame it on lib/reporters/Progress.js , which swallows successful test results and spits out a summary line for each browser. If you're determined, though, you have (at least) two non-trivial ways (in v0.9.2 ) of getting a "good enough" result

Why can't nested describe() blocks see vars defined in outer blocks?

我与影子孤独终老i 提交于 2019-12-03 04:30:34
问题 I've run into this issue in real code, but I put together a trivial example to prove the point. The below code works fine. I've set up a variable in my root describe() block that is accessible within my sub- describe() s' it() blocks. describe('simple object', function () { var orchard; beforeEach(function () { orchard = { trees: { apple: 10, orange : 20 }, bushes: { boysenberry : 40, blueberry: 35 } }; }); describe('trees', function () { it ('should have apples and oranges', function() { var

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

旧巷老猫 提交于 2019-12-03 04:25:09
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? Thanks. To test asynchronous code with jasmine you should use its async syntax , e.g.: describe('test

KarmaJS, Jasmine, RequireJS, etc: How to Use Require for Testing Modules

断了今生、忘了曾经 提交于 2019-12-03 04:18:57
问题 Running Karma + Jasmine Tests with RequireJS -- Getting off the ground Help! . . . _ _ _ . . . SOS! Currently, I have an exercise project up for getting comfortable with KarmaJS -- and Unit Testing, at large. The broad issue is that I really have no transparent view of what Karma is doing behind the scenes, and I can't seem to find adequate documentation in relevant areas. Without further delay... Here is my folder structure : root |-/lib |-/[dependencies] (/angular, /angular-mocks,