简体   繁体   English

使用Jasmine单元测试测试Angular模块

[英]Testing Angular Modules with Jasmine Unit Tests

I am using an AngularJS service in my app. 我在我的应用程序中使用AngularJS服务。 I've learned that the service I've created is unreliable. 我了解到我创建的服务不可靠。 For that reason, I want to build some unit tests around it. 因此,我想围绕它构建一些单元测试。 While the service works fine in my AngularJS app, I'm having problems getting it to work with Jasmine. 虽然该服务在我的AngularJS应用程序中可以正常工作,但在使其与Jasmine一起使用时遇到了问题。 Its like I can't load any modules. 就像我无法加载任何模块。 I've strip down the code to the bare bones. 我已经将代码精简了。 I do not understand what I'm doing wrong. 我不明白我在做什么错。 My code looks like this: 我的代码如下所示:

myService.js myService.js

'use strict';
angular.module('customModule', [])
    .factory('$serviceName', [function () {    
        return {    
            isAvailable: function () {
                return true;
            }
        };
    }]
);

myService.spec.js myService.spec.js

  describe('customModule', function() {
    beforeEach(function() {
        console.log('loading module...');
        module('customModule');
    });

    describe('$serviceName', function () {
        var myService = null;
        beforeEach(inject(function ($serviceName) {
            console.log('loading service...');
            myService = $serviceName;
        }));

        it('should work', function () {
            console.log('testing');
            var isAvailable = myService.isAvailable();
            console.log(isAvailable);
            expect(1 + 2).toEqual(3);
        });
    });
  })

gruntfile.js gruntfile.js

'use strict';
module.exports = function (grunt) {
  grunt.initConfig({
    jasmine: {
      unitTests: {
        src: 'test/*.js',
      }
    }
  });

  require('load-grunt-tasks')(grunt);
  grunt.registerTask('default', ['jasmine:unitTests']);
};

My jasmine tests are running. 我的茉莉花测试正在运行。 However, its like myService.js isn't being loaded. 但是,未加载类似myService.js的文件。 I'm not sure how to do that. 我不确定该怎么做。 I'm also not sure how to get 'angular' (used in myService.js) in the tests either. 我也不确定如何在测试中获得“角度”(在myService.js中使用)。

Thank you for your help. 谢谢您的帮助。

Do you load you Angular application in myService.spec.js? 您是否在myService.spec.js中加载了Angular应用程序? Otherwise $serviceName is not available for dependency injection. 否则,$ serviceName不可用于依赖项注入。

You should add something like this in your test file: 您应该在测试文件中添加以下内容:

beforeEach(module('angularApp'));

(of course replace angularApp with the name you are using) (当然,将angularApp替换为您使用的名称)

And it is also a good idea to use another beforeEach block to handle the dependency injection so you can use it in all tests in myService.spec.js. 另外,最好使用另一个beforeEach块来处理依赖项注入,以便可以在myService.spec.js中的所有测试中使用它。 In total you should have something like this: 总的来说,您应该有以下内容:

describe('serviceName', function () {

  beforeEach(module('angularApp'));

  var iMyService

  // Initialize the service
  beforeEach(inject(function ($serviceName) {
      iMyService = $serviceName;
  }));

  it('should check if available', function () {
    var isAvailable = iMyService.isAvailable();
    console.log(isAvailable);
    expect(1 + 2).toEqual(3);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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