jasmine

How to assert a spy is invoked with event on click using jasmine?

孤人 提交于 2019-12-03 11:21:57
I'm writing a simple click handler and need the event passed in (like so) Thing = function($){ var MyObject = function(opts){ this.opts = opts; }; MyObject.prototype.createSomething = function(){ var that = this; $('#some_dom_element').live('click', function(e) { that.doStuff(e); }); }; MyObject.prototype.doStuff = function(e) { //do some javascript stuff ... e.preventDefault(); }; return MyObject; }(jQuery); Currently in my jasmine spec I've got something to spy on the function I expect gets invoked (but since it's called with e -not without args- my assertion is failing) it ("live click

Mocking router.events.subscribe() Angular2

亡梦爱人 提交于 2019-12-03 11:12:40
问题 In my app.component.ts I have the following ngOnInit function: ngOnInit() { this.sub = this.router.events.subscribe(e => { if (e instanceof NavigationEnd) { if (!e.url.includes('login')) { this.loggedIn = true; } else { this.loggedIn = false; } } }); } Currently I'm testing if the sub is not null but I want to test the function with a 100% coverage. I want to mock the router object so that I can simulate the URL and then test if the this.loggedIn is correctly set. How would I proceed to mock

Testing for focus an AngularJS directive

风格不统一 提交于 2019-12-03 10:58:26
问题 How can you test for focus in an AngularJS directive? I would expect the following to work: describe('focus test', function(){ it('should focus element', function(){ var element = $('<input type="text" />'); // Append to body because otherwise it can't be foccused element.appendTo(document.body); element.focus(); expect(element.is(':focus')).toBe(true); }); }); However, this only works in IE, it fails in Firefox and Chrome Update: The solution by @S McCrohan works. Using this I created a

How can I have a beforeAll function in Jasmine ? (Not coffeeScript)

蹲街弑〆低调 提交于 2019-12-03 10:48:09
问题 I need to know if there is a way to include or use a beforeAll function, or something similar, so I can login to my application and then start testing. Right now I'm putting my login operations in the first test case ( it ). Which is not a good practice. If there is a better way to store my login code other then using a beforeAll function please tell me about it. I'm using pure Jasmine not related to any other framework like coffee-script or others. Thank you 回答1: This is now much easier. As

AngularJS Promise Callback Not Trigged in JasmineJS Test

放肆的年华 提交于 2019-12-03 10:29:11
问题 Problem Intro I'm trying to unit test an AngularJS service that wraps the Facebook JavaScript SDK FB object; however, the test isn't working, and I haven't been able to figure out why. Also, the service code does work when I run it in a browser instead of a JasmineJS unit test, run with Karma test runner. I'm testing an asynchronous method using Angular promises via the $q object. I have the tests set up to run asynchronously using the Jasmine 1.3.1 async testing methods, but the waitsFor()

One of strings in array to match an expression

和自甴很熟 提交于 2019-12-03 10:13:04
The Problem: I have an array of promises which is resolved to an array of strings . Now the test should pass if at least one of the strings matches a regular expression. Currently, I solve it using simple string concatenation: protractor.promise.all([text1, text2, text3]).then(function (values) { expect(values[0] + values[1] + values[2]).toMatch(/expression/); }); Obviously, this does not scale well and is not particularly readable. The Question: Is is possible to solve it using a custom jasmine matcher , or jasmine.any() or custom asymmetric equality tester ? You could simply use map to get a

How to unit test a FormControl in Angular2

血红的双手。 提交于 2019-12-03 10:11:55
My method under test is the following: /** * Update properties when the applicant changes the payment term value. * @return {Mixed} - Either an Array where the first index is a boolean indicating * that selectedPaymentTerm was set, and the second index indicates whether * displayProductValues was called. Or a plain boolean indicating that there was an * error. */ onPaymentTermChange() { this.paymentTerm.valueChanges.subscribe( (value) => { this.selectedPaymentTerm = value; let returnValue = []; returnValue.push(true); if (this.paymentFrequencyAndRebate) { returnValue.push(true); this

AngularJS + Jasmine mock unit test $http factory of POST method

匿名 (未验证) 提交于 2019-12-03 09:52:54
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How I can make unit-test of $http factory, if it using post method, for example: // controller $scope.logOut = function (){ logOutFactory.logOut().then(function(resp){ }); }; // service app.factory('logOutFactory', ['$http', '$q', 'CONST', function ($http, $q, CONST){ var logoutApiUrl = CONST.URL+'logout'; return { logOut: function() { var deferred = $q.defer(); $http({ method: "post", url: logoutApiUrl }) .success(function (data) { deferred.resolve(data); }) .error(function (data) { deferred.reject('error in $http request'); console.log

Testing Web Sockets with Jasmine

回眸只為那壹抹淺笑 提交于 2019-12-03 09:45:06
Here is some code that has been written for web-socket using stomp protocol. function WS(url) { var ws = new SockJS('/notifications'); this.client = Stomp.over(ws), this.client.connect('', '', function() { console.log('Connected'); }, function(error) { console.log('STOMP protocol error: ', error.headers.message); }); } WS.prototype.disconnect = function() { }; WS.prototype.subscribe = function() { }; WS.prototype.unSubscribe = function() { }; WS.prototype.send = function(msg) { }; I found this post but it requires actual connection to server, Unit testing Node.js and WebSockets (Socket.io) How

AngularJS Jasmine Test Fails: Failed to instantiate module

折月煮酒 提交于 2019-12-03 09:41:51
My angular app worked great and so did my tests, using karma and jasmine, until I added a dependency in ui.bootstrap . Now the app still works as expected, but I can't get my tests to run. This is what I have: app.js - added dependency in ui.bootstrap angular.module('myApp', ['ngResource', 'ngRoute', 'ui.bootstrap']).config(function(...) {...}); service.js angular.module('myApp').service('myService', function () {}) controller.js angular.module('myApp').controller('MyController', function ($scope, $http, myService) {}) tests/main.js describe('Controller: MyController', function () { var