简体   繁体   中英

Unit Testing + Jasmine + Angular

i am getting all the time "encountered a declaration exception". I am trying to write my first unit test for a factory. It seems like mocking doesn't really work.

The paths to do references look like this:

///<reference path="~/App/js/jasmine/jasmine.js"/>
///<reference path="~/App/js/lib/40_Angular/angular.js"/>
///<reference path="~/App/js/jasmine/angular-mocks.js"/>

///<reference path="~/App/app.js"/>
///<reference path="~/App/services/userService.js"/>

My sample tests look like this:

describe("userService", function () {

 beforeEach(module("issApp"));

 describe("userService", function () {

 var userService;

 beforeEach(inject(function () {
  userService = $injector.get('userService');
 }));

 it('should be true', function () {
  expect(true).toBeDefined();
 });

 it('should have a farbSchema', function () {
  expect(userService.getParameter("ngFarbSchema")).toBeDefined();
 });
}); 

My app.js is declared like this:

iss.app = angular.module("issApp", ["ng", "ui.router" ...]);

userService.js is declared like this:

iss.app.factory("userService", function ($q, $http) {
 return {
    getParameter: getParameter,
    setParameter: setParameter,
    ...
 };
 function getParameter(parameterName) {
    var deferred = $q.defer();

    $http({
        url: iss.urls.base + iss.urls.getuserparam,
        method: "GET",
        params: { parameterName: parameterName }
    })
    .success(function (data) {
        deferred.resolve(data);
    })
    .error(function (error) {
        console.log("Fehler beim Aufruf userService.getParameter(): " + JSON.stringify(error));
    });

    return deferred.promise;
  }
  ...
});

This are the version of the tools I use:

Resharper: 8.2.1

Angular: 1.5.8

Visual Studio: 2013

Jasmine: 2.4.1

expect(userService.getParameter("ngFarbSchema")).toBeDefined();

i dont think it is good to check if method is defined that way. better way is to check method without call it:

expect(userService.getParameter).toBeDefined();

look at this plnkr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM