简体   繁体   English

服务/工厂的单元测试 - AngularJS - Jasmine

[英]Unit testing of Services / Factories - AngularJS - Jasmine

I have some troubles writing tests for my services in my angularJS project. 我在angularJS项目中为我的服务编写测试时遇到了一些麻烦。 I am using Karma and Jasmine for my unit tests. 我正在使用Karma和Jasmine进行单元测试。 To begin, I chose a service with no dependencies, but I never had passed the tests. 首先,我选择了一个没有依赖关系的服务,但我从未通过测试。

Here's my service ( written with coffeeScript ) 这是我的服务(用coffeeScript编写)

angular.module('app').factory 'rankFactory', [ ->
  rankFactory = {}
  ranks = [
    {
      id: 0
      label: 'RANK0'
    }
    {
      id: 1
      label: 'RANK1'
    }
  ]

  rankFactory.getRanks = ->
    ranks

  rankFactory.getRanks = (id) ->
    ranks[id]

  rankFactory
 ]

The service works fine. 服务很好。 Thus, the test doesn't. 因此,测试没有。 Here's my test : 这是我的测试:

describe('rank Factory unit tests', function(){
    describe  ('when I call myService rankFactory.getRanks ()', function() {

        beforeEach(module('app'));

            it('returns ranks', inject(function(rankFactory){
                expect(rankFactory.getRanks()).not.to.equal(null);
            }))
        }
    )
});

I have been trying for multiple hours, and read a lot of issues and documentation but still can not find out why it doesn't work. 我一直在尝试多个小时,并阅读了很多问题和文档,但仍然无法找出它为什么不起作用。 Can you help me please? 你能帮我吗?

----------------------------------------------------------------EDIT----------------------------------------------------------- -------------------------------------------------- - - - - - - - 编辑 - - - - - - - - - - - - - - - - - - ------------------------

I figured out that my issue is related with coffeeScript. 我发现我的问题与coffeeScript有关。 My Controllers, services are written with coffeeScript and when I launch my tests, I got Syntax errors related to my services. 我的控制器,服务是用coffeeScript编写的,当我启动测试时,我得到了与我的服务相关的语法错误。

Here is my Config file : 这是我的Config文件:

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
        '../bower_components/angular/angular.js',
        '../bower_components/angular-ui-router/release/angular-ui-router.js',
        '../bower_components/angular-mocks/angular-mocks.js',
        '../src/scripts/**/*.coffee',
        '../src/scripts/Services/rankService.coffee',
        'unit-tests/**/*.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        '**/*.coffee': ['coffee']
    },

    coffeePreprocessor: {
      // options passed to the coffee compiler
      options: {
        bare: true,
        sourceMap: false
      },
      // transforming the filenames
      transformPath: function(path) {
        return path.replace(/\.coffee$/, '.js')
      }
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_DEBUG,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

I am writing my tests in javaScript and I am confused what do I have to do to make it cover CoffeeScript. 我正在用javaScript编写我的测试,我很困惑我需要做些什么来使它覆盖CoffeeScript。

Ps : I have installed karma-coffee-preprocessor Ps:我已经安装了karma-coffee-preprocessor

Given the following example from this tutorial : 给出本教程中的以下示例:

describe('Chats Unit Tests', function() {
    var Chats;
    beforeEach(module('starter.services'));

    beforeEach(inject(function(_Chats_) {
        Chats = _Chats_;
    }));

    it('can get an instance of my factory', inject(function(Chats) {
        expect(Chats).toBeDefined();
    }));

    it('has 5 chats', inject(function(Chats) {
        expect(Chats.all().length).toEqual(5);
    }));
});

I would deduct that you need to do something like: 我会扣除你需要做的事情:

describe('rank Factory unit tests', function(){
    var factory;
    beforeEach(module('app'));

    beforeEach(inject(function(_rankFactory_) {
        factory = _rankFactory_;
    }));

    it('returns ranks', inject(function(factory) {
        expect(factory.getRanks()).not.to.equal(null);
    }));
});

Hope this helps. 希望这可以帮助。

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

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