Why use spyOn instead of jasmine.createSpy?

The difference is that you should have a method on the object with spyOn const o = { some(): { console.log(‘spied’) } }; spyOn(o, ‘some’); while the mock method is created for your with createSpy(): const o = {}; o.some = jasmine.createSpy(‘some’); The advantage of the spyOn is that you can call the original method: … Read more

Angular 2: How to mock ChangeDetectorRef while unit testing

Update 2020: I wrote this originally in May 2017, it’s a solution that worked great at the time and still works. We can’t configure the injection of a changeDetectorRef mock through the test bed, so this is what I am doing these days: it(‘detects changes’, () => { // This is a unique instance here, … Read more

Is there a way to add a Jasmine matcher to the whole environment

Sure, you just call beforeEach() without any spec scoping at all, and add matchers there. This would globally add a toBeOfType matcher. beforeEach(function() { var matchers = { toBeOfType: function(typeString) { return typeof this.actual == typeString; } }; this.addMatchers(matchers); }); describe(‘Thing’, function() { // matchers available here. }); I’ve made a file named spec_helper.js full … Read more

How do I trigger a keyup/keydown event in an angularjs unit test?

You need to create an event programatically and trigger it. To do so including jQuery for unit tests is quite useful. For example, you could write a simple utility like this: var triggerKeyDown = function (element, keyCode) { var e = $.Event(“keydown”); e.which = keyCode; element.trigger(e); }; and then use it in your unit test … Read more

Is there something like a jasmine `toNotContain` acceptance criteria for tests?

According to the documentation, you can use not: getJasmineRequireObj().not.toContain This example is from here: describe(“The ‘toBe’ matcher compares with ===”, function() { Matchers Each matcher implements a boolean comparison between the actual value and the expected value. It is responsible for reporting to Jasmine if the expectation is true or false. Jasmine will then pass … Read more

Mocking router.events.subscribe() Angular2

I have found the answer, if someone is looking for it: import { NavigationEnd } from ‘@angular/router’; import { Observable } from ‘rxjs/Observable’; class MockRouter { public ne = new NavigationEnd(0, ‘http://localhost:4200/login’, ‘http://localhost:4200/login’); public events = new Observable(observer => { observer.next(this.ne); observer.complete(); }); } class MockRouterNoLogin { public ne = new NavigationEnd(0, ‘http://localhost:4200/dashboard’, ‘http://localhost:4200/dashboard’); public … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)