Injecting a mock into an AngularJS service

You can inject mocks into your service by using $provide. If you have the following service with a dependency that has a method called getSomething: angular.module(‘myModule’, []) .factory(‘myService’, function (myDependency) { return { useDependency: function () { return myDependency.getSomething(); } }; }); You can inject a mock version of myDependency as follows: describe(‘Service: myService’, function … Read more

How can I test that a value is “greater than or equal to” in Jasmine?

I figured I should update this since the API has changed in newer versions of Jasmine. The Jasmine API now has built in functions for: toBeGreaterThanOrEqual toBeLessThanOrEqual You should use these functions in preference to the advice below. Click here for more information on the Jasmine matchers API I know that this is an old … Read more

Unit Testing AngularJS directive with templateUrl

You’re correct that it’s related to ngMock. The ngMock module is automatically loaded for every Angular test, and it initializes the mock $httpBackend to handle any use of the $http service, which includes template fetching. The template system tries to load the template through $http and it becomes an “unexpected request” to the mock. What … Read more

How to have different return values for multiple calls on a Jasmine spy

You can use spy.and.returnValues (as Jasmine 2.4). for example describe(“A spy, when configured to fake a series of return values”, function() { beforeEach(function() { spyOn(util, “foo”).and.returnValues(true, false); }); it(“when called multiple times returns the requested values in order”, function() { expect(util.foo()).toBeTruthy(); expect(util.foo()).toBeFalsy(); expect(util.foo()).toBeUndefined(); }); }); There is some thing you must be careful about, there … Read more

How can I get WebStorm to recognize Jasmine methods?

You can use predefined JS library stubs in Webstorm/PHPStorm/Idea Open File > Settings… Select Languages & Frameworks > JavaScript > Libraries Click on Download… Swich to TypeScript community stubs Find karma-jasmine (originally under the name jasmine) (If this does not work, try jasmine instead) Click on Download and Install I am using this setup with … Read more

How to spyOn a value property (rather than a method) with Jasmine

In February 2017, they merged a PR adding this feature, they released in April 2017. so to spy on getters/setters you use: const spy = spyOnProperty(myObj, ‘myGetterName’, ‘get’); where myObj is your instance, ‘myGetterName’ is the name of that one defined in your class as get myGetterName() {} and the third param is the type … Read more

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