jasmine

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

不问归期 提交于 2019-11-30 14:47:27
问题 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

Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression?

瘦欲@ 提交于 2019-11-30 14:35:42
问题 I have reviewed Jasmine's documentation of the toHaveBeenCalledWith matcher in order to understand whether it's possible to pass in a regular expression for an argument, if that argument is expected to be a string. Unfortunately, this is unsupported functionality. There's also an issue open on github requesting this functionality. I've dug a bit into the codebase, and I see how it might be possible to implement this inside the existing matcher. I think it would be more appropriate to

Stubbing WebSocket in JavaScript with Jasmine

喜你入骨 提交于 2019-11-30 14:18:48
I try to test if onmessage is a proper function. Here is a test: describe(".init(address, window)", function() { beforeEach(function() { address = 'ws://test.address'; window = {}; e = { data: {} } spyOn(window, 'WebSocket').and.returnValue(function() {return {onmessage: null}}); spyOn(subject, 'handleMessage'); }); it("should create a WebSocket client which connects to the given address", function() { subject.init(address, window); expect(window.WebSocket).toHaveBeenCalledWith(address); }); it("should have onmessage method overriden with a function which handles message", function() { ws =

AngularJS ui-router: Test ui-sref

别说谁变了你拦得住时间么 提交于 2019-11-30 13:50:27
I'm trying to test some views, that are using <a ui-sref='someState'>link</a> to link to other states in my application. In my tests I'm triggering a click on this elements like this: element.find('a').click() How do I test, if the state is switched to someState ? It would be easy, when using $state in my controller like this: // in my view <a ng-click="goTo('someState')">link</a> // in my controller $scope.goTo = function(s) { $state.go(s) }; // in my tests spyOn($state, 'go'); element.find('a').click() expect($state.go).toHaveBeenCalled() But when I use ui-sref I don't know what object to

How do I mock RxJs 6 timer?

折月煮酒 提交于 2019-11-30 13:40:15
We've recently updated from Angular 5 to Angular 6, and with it RxJs 6. As part of the migration, the timer usage has changed from: Observable.timer() to timer() There are a number of places in our tests where we mock timer observables with the following pattern. let timerObserver: Observer<any>; beforeEach(() => { spyOn(Observable, 'timer').and.returnValue(Observable.create( ((observer: Observer<any>) => { timerObserver = observer; }) )); }); it(`should not have any notifications by default`, () => { timerObserver.next(''); ... }); Does anybody know how to migrate this pattern across? Edit: I

Testing an AngularJS factory with Karma (Jasmine)

筅森魡賤 提交于 2019-11-30 13:29:31
I'm struggling with testing an AngularJS factory using Karma + Jasmine. I am unable to inject my factory to OfficerValidationService variable. What am i doing wrong? Note: the file is loaded properly Factory: 'use strict'; angular.module('darthvader').factory('OfficerValidationService', [function(){ var OfficerValidationService = {}; OfficerValidationService.something = function() { return true; }; return OfficerValidationService; }]); Code: 'use strict'; (function() { describe('OfficerValidationService Spec', function() { var OfficerValidationService; beforeEach(function() { angular.module(

jasmine test fails with undefined is not a function(evaluating $browser.$$checkUrlChange())

╄→гoц情女王★ 提交于 2019-11-30 13:11:28
问题 I have a following controller: .controller('ProjectUserAddCtrl', ['$scope', 'ProjectUser', '$q', 'i18nNotifications', function($scope, ProjectUser, $q, i18nNotifications) { var buildUnassignedUsers = function(users, project) { var unassignedUsers = []; angular.forEach(users, function(user) { var match; angular.forEach(project.projectUsers, function(projectUser) { if(match) {return;} if(projectUser.user.id === user.id) { match = true; } }); if(!match) { unassignedUsers.push(user); } }); $scope

backbone.js click event spy is not getting called using jasmine.js and sinon.js

六月ゝ 毕业季﹏ 提交于 2019-11-30 12:36:55
问题 I am trying to test a button click using backbone.js, jasmine.js and sinon.js. But the following test case fails. I am using a spy to track whether it is getting called or not. Can you please help me with this? Thanks. New Task Template <script id='new_task_template' type='text/template'> <input type='text' id='new_task_name' name='new_task_name'></input> <button type='button' id='add_new_task' name='add_new_task'>Add Task</button> </script> NewTaskView T.views.NewTaskView = Backbone.View

How to fake return calls from the geolocator in jasmine

流过昼夜 提交于 2019-11-30 12:17:28
I have a function that calls the geolocator and i don't know how to test this function. I've tried spying on the geolocator and returning fake data but with no success, the original function is still used and so i would have to wait and i couldn't use mock data. // this doesn't work var navigator_spy = spyOn( navigator.geolocation, 'getCurrentPosition' ).andReturn( { coords : { latitude : 63, longitude : 143 } } ); How can I do this? When you call the geolocation code, it looks like this: navigator.geolocation.getCurrentPosition(onSuccess, onError); This means that you're calling it and

Angular Protractor e2e Testing

北慕城南 提交于 2019-11-30 12:09:49
I am writing an end-to-end test using Protractor for my Angular application. I can mock httpBackend for unit test but I want to actually call the server and get the JSON response back and write tests again the data returned. I have read a lot on stackoverflow but can not understand how this is done. Do I use $http? How do I inject it into my Jasmine tests? How do I get the response JSON back into my Jasmine test? any help or links to resources with instructions on doing this will be helpful. Again I do NOT want to mock to server, I want to hit the server and get the JSON back. Thanks! I'm