Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Jasmine模拟AngularJS服务_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

用Jasmine模拟AngularJS服务

用Jasmine模拟AngularJS服务,angularjs,unit-testing,jasmine,Angularjs,Unit Testing,Jasmine,在我的控制器中模拟我的服务似乎有困难。特别是“时刻服务”。这是我的测试代码 describe('Moment Controller', function() { var $scope, $controller, momentsService, $ionicContentBanner, core, components, $q, $ionicPopup; beforeEach(module('app')); beforeEach(module('core'));

在我的控制器中模拟我的服务似乎有困难。特别是“时刻服务”。这是我的测试代码

describe('Moment Controller', function() {

    var $scope, $controller, momentsService, $ionicContentBanner, core, components, $q, $ionicPopup;

    beforeEach(module('app'));
    beforeEach(module('core'));
    beforeEach(module('components'));
    beforeEach(module('constants'));


    beforeEach(inject(function(_$controller_, _$rootScope_, _momentsService_, _$ionicContentBanner_, _core_, _components_, _$q_, _$ionicPopup_) {
        $scope = $rootScope.$new();
        momentsService = _momentsService_;
        $ionicContentBanner = _$ionicContentBanner_;
        core = _core_;
        components = _components_;
        $q = _$q_;
        $ionicPopup = _$ionicPopup_;

        spyOn(momentsService, 'initializeView');

        $controller = _$controller_('MomentsController', {
            momentsService: momentsService, 
            $scope: $scope, 
            $ionicContentBanner: $ionicContentBanner,
            core: core,
            component: components,
            $q: $q,
            $ionicPopup: $ionicPopup
        });
    }));

it('should initialize', function() {
    console.log("CONTROLLER");
    console.log(momentsService);
    console.log(momentsService.initializeView());
});

});
最后3个console.log一直给我提供未定义的“momentsService”。在终端中,我得到了“undefined is not a object”(未定义不是对象),因此我的服务没有被正确地模拟,并且我的测试一直失败

控制器和依赖项:

(function() {
    angular.module('app.MomentsController', [])

    .controller('MomentsController', ['momentsService', '$scope', '$ionicContentBanner', 'core', 'components', '$q', '$ionicPopup', MomentsController]);

    function MomentsController (momentsService, $scope, $ionicContentBanner, core, components, $q, $ionicPopup) {
var vm = this;
//Omitted
(function() {
    angular.module('app.momentsService', [])

    .service('momentsService', ['core', '$q', 'constants', 'awsServices', 'components', 'logger', 'geolocation', momentsService]);

    function momentsService(core, $q, constants, awsServices, components, logger, geolocation){
我错过了什么

谢谢你的帮助

-马特

编辑: 如果有必要的话,我正在使用controllerAs语法

编辑2: 尽管尝试了两种修复方法,但这对我仍然不起作用。momentsService尚未定义。我的守则如下:

describe('Moment Controller', function() {

    var $scope, $controller, momentsService, $ionicContentBanner, core, components, $q, $ionicPopup;

    beforeEach(module('app'));
    beforeEach(module('core'));
    beforeEach(module('components'));
    beforeEach(module('constants'));


    beforeEach(inject(function(_$controller_, $rootScope, _momentsService_, _$ionicContentBanner_, _core_, _components_, _$q_, _$ionicPopup_) {
        var self = this;
        this.$scope = $rootScope.$new();
        this.momentsService = _momentsService_;

        this.$ionicContentBanner = _$ionicContentBanner_;
        this.core = _core_;
        this.components = _components_;
        this.$q = _$q_;
        this.$ionicPopup = _$ionicPopup_;

        spyOn(momentsService, 'initializeView');

        this.$controller = _$controller_('MomentsController', {
            momentsService: momentsService, 
            $scope: $scope, 
            $ionicContentBanner: $ionicContentBanner,
            core: core,
            component: components,
            $q: $q,
            $ionicPopup: $ionicPopup
        });
    }));

    it('should initialize', function() { 
        console.log(this);
        console.log("CONTROLLER");
        console.log(this.momentsService);
        console.log(this.momentsService.initializeView());
    });

});
我也尝试过不使用这个,因为在评论中发布的plunker并没有使用它。如果您忽略了这个代码与plunker发布的代码完全匹配,我只是添加了其他依赖项——当我注释掉它时,它仍然抱怨我的服务未定义。我不明白

编辑3:

conf.js中的“文件”:

  files: [
      'lib/ionic/js/ionic.bundle.js',
      'lib/angular-mocks/angular-mocks.js',
      'js/*',
      'bestMoments/*',
      'moments/*',
      'submitMoment/*',
      'myMoments/*',
      'index.controller.js',
      'layout/mainView.directive.js',
    ],
.Service.js依赖项:

(function() {
    angular.module('app.MomentsController', [])

    .controller('MomentsController', ['momentsService', '$scope', '$ionicContentBanner', 'core', 'components', '$q', '$ionicPopup', MomentsController]);

    function MomentsController (momentsService, $scope, $ionicContentBanner, core, components, $q, $ionicPopup) {
var vm = this;
//Omitted
(function() {
    angular.module('app.momentsService', [])

    .service('momentsService', ['core', '$q', 'constants', 'awsServices', 'components', 'logger', 'geolocation', momentsService]);

    function momentsService(core, $q, constants, awsServices, components, logger, geolocation){
文件结构:

假设jasmine2-注意所有带有self的行,这就是。beforeXXXX中的任何属性在其内部和afterXXXX上都可用

在您的情况下,未定义的是console.log中的momentService,其中未定义momentService。jasmine特性解决了这个问题


你的plunker不适用于我,你能检查我的新代码吗?矩服务在.app.times中注册的模块是什么。我也尝试在我的测试中注入app.moments,但没有成功。您遇到了什么错误?app.moments肯定需要注入,也许你的karma.conf.js需要更新你的建议对我不起作用。你能看看我的编辑,看看哪里出错了吗?我想你的问题可能是因为定义了angular.module'app.momentsService',[]两次。秒发生应为angular.module'app.momentsService',即不传递模块依赖项,以便angular将返回先前创建的app.momentService。如果这没有任何帮助,我建议您按照@Bowofola的建议创建jsfiddle并重新创建问题?经过一些调查,它似乎根本没有击中我的beforeach循环。知道为什么吗?使用Chrome或某些浏览器,然后单击Debug-然后单击F12并查看是否有任何错误-ypu可以通过放置断点在浏览器中进行调试。