简体   繁体   中英

testing angular services with mocha

I've been trying to unit test some angular js services using Mocha by injecting mock dependencies. I've read a lot about this and there is some suggestion that using the master branch of angular-mocks should work fine with mocha. However, when I try this, I get the follwing javascript error and the 'inject' and 'module' functions are undefined:

ReferenceError: beforeEach is not defined

I've also tried using angular.injector, which has led me to some success, however, I am unable to mock the services which my service depend on (in this case $http). My service:

var app = angular.module('app', ['ui.bootstrap'], function($provide){
    $provide.factory('myService', ['$http', function($http){
        return { 
            doSomething: function(batch){
                ...
            }
        };
    }]);
});

Resolving:

$injector = angular.injector(['ng', 'app']);
var subject = $injector.get('myService');

Is this possible without using jasmine?

The angular-mocks shipped with all AngularJS versions < 1.2.0 is not compatible with mocha. Use the bower version of angular-mocks instead: https://github.com/angular/bower-angular-mocks

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