jasmine

Trying to understand Jasmine's toHaveBeenCalled() matcher

人盡茶涼 提交于 2019-12-06 18:38:23
问题 I am new to jasmine here is my src file in which i create Auth class function Auth() { } Auth.prototype.isEmpty = function(str) { return (!str || 0 === str.length); } Auth.prototype.Login = function (username , password) { if (this.isEmpty(username) || this.isEmpty(password)) { return "Username or Password cann't be blank "; } else { return "Logged In !"; } } now i want to test jasmine's toHaveBeenCalled() matcher . Here is what i write it("should be able to Login", function () { spyOn

How to unit test Angular controller with $scope.$on?

痴心易碎 提交于 2019-12-06 18:31:54
问题 I have a controller with an event listener in my Angular app, defined as follows. angular.module('test').controller('TestCtrl', function($rootScope, $scope, testService) { [...] $scope.$on("myEvent", function(args, value) { testService.doStuff(value); }); }); This works perfectly in the app itself. However, when I try to unit test the functionality of the controller (using Jasmine and Karma ), each test method throws the following error: TypeError: $scope.$on is not a function in [...]

Jasmine spies callThrough and callFake

回眸只為那壹抹淺笑 提交于 2019-12-06 17:56:32
问题 I have a scenario whereby I want to call done() on a beforeEach after a callback has been invoked. I tried to do this as follows : spyOn(scope, 'onAdmin').and.callThrough().and.callFake(function(){done()}) But I'm not sure I get the right behaviour. Essentially what I want to achieve is to be able to call done() after each callback is done doing what it does. UPDATE: workaround solution scope.onAdminBackup = scope.onAdmin; spyOn(scope, 'onAdmin').and.callFake(function(admin) { scope

Template parse error in Jasmine test but not actual app

谁都会走 提交于 2019-12-06 17:10:56
问题 I've developed a Jasmine spec to test an angular 2 component MiddleRowComponent . When I run the jasmine test, it gives this error: zone.js:388 Unhandled Promise rejection: Template parse errors: 'circles' is not a known element: 1. If 'circles' is an Angular component, then verify that it is part of this module. 2. If 'circles' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("</div> <div class="col-md-10 col-sm-12

How to test angular 2 component with nested components inside with their own dependencies ? (TestBed.configureTestingModule)

℡╲_俬逩灬. 提交于 2019-12-06 16:44:44
问题 I have a component A that use a component B,c,D in its template: ###template-compA.html <comp-b></comp-b> <comp-c [myinput]="obj.myinput"></comp-c> <comp-d ></comp-d> ...etc To simplify, let's say their is only one directive in component A: ###template-compA.html <comp-b></comp-b> My comp-b has its own dependencies (services or other comp). If I want to test comp-a this way: TestBed.configureTestingModule({ declarations: [comp-A], imports: [ReactiveFormsModule], }).overrideComponent

how to skip to next describe() upon error in previous describe? jasmine test

时光总嘲笑我的痴心妄想 提交于 2019-12-06 15:56:28
i'm using such code in order to stop the entire describe upon an error in one of the it() functions: if(this.results_.failedCount > 0) { //next step will finish the test jasmine.Queue.prototype.next_ = function () { // to instead skip to the end this.onComplete(); } } i got it from: How can I make jasmine.js stop after a test failure? however, if i have few describe blocks in the file, and the first describe fails, i would like it to continue to the next describe and not kill the entire test. how can i do it? thanks omer727 Asher, I've used your code from the workaround inside afterEach and it

Is using javascript setters and getter properties to watch for changes a bad idea?

折月煮酒 提交于 2019-12-06 15:46:04
Using Object.create one can define setter and getter methods in javascript. o = Object.create(Object.prototype, { fooBar: { get: function() { return "Meh" }, set: function(value) { console.log(value) } } }); console.log(o) will always return Meh, and doing o = "Heyo" would only output the new value directly without changing o . Is there any reason as to why I should not use, or rely on this behavior to trigger events? What about two-way binding frameworks like angular and ember, do they make use of this feature? What about unit-testing? What is the defacto standard way of watching for changes

Spy and callThrough native WebSocket constructor with Jasmine

情到浓时终转凉″ 提交于 2019-12-06 14:10:45
I'm trying to spy on native WebSocket constructor, so I wrote: it("should spy and call through WebSocket constructor", function (done) { var WSspy = spyOn(window, "WebSocket").and.callThrough(); var ws = new WebSocket("ws://some/where"); expect(WSspy).to.toHaveBeenCalledWith("ws://some/where"); }); but it results in error: TypeError: Failed to construct 'WebSocket': Please use the 'new' operator, this DOM object constructor cannot be called as a function. How should I callThrough such constructor? I found a workaround with callFake : it("should spy and callFake WebSocket constructor", function

not.custom_matcher error - Jasmine

别等时光非礼了梦想. 提交于 2019-12-06 13:26:09
I have created a custom matcher in jasmine to verify if an element has a class or not. This is the code that I have placed in the beforeEach() function to define the custom matcher: beforeEach(function() { jasmine.addMatchers({ toHaveClass: function() { return { compare: function(actual, expected) { return { pass: actual.getAttribute('class').then(function(classes) { return classes.split(' ').indexOf(expected) !== -1; }) } } } } }); this.driver = new webdriver.Builder().forBrowser('firefox').build(); this.driver.manage().window().maximize(); this.driver.get('http://localhost:8000/'); }); Then

Unit testing logic inside promise callback

£可爱£侵袭症+ 提交于 2019-12-06 13:09:13
I have an ES6 / Aurelia app that I am using jasmine to test. The method I am trying to test looks something like this: update() { let vm = this; vm.getData() .then((response) => { vm.processData(response); }); } Where this.getData is a function that returns a promise. My spec file looks something like this: describe('my service update function', () => { it('it will call the other functions', () => { myService = new MyService(); spyOn(myService, 'getData').and.callFake(function() { return new Promise((resolve) => { resolve(); }); }); spyOn(myService, 'processData').and.callFake(function() {