1

我在我的angularJS项目中为我的服务编写测试有一些麻烦。 我使用Karma和Jasmine进行单元测试。 首先,我选择了一个没有依赖关系的服务,但我从未通过过测试。服务/工厂单元测试 - AngularJS - Jasmine

这里是我的服务(以书面的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 
] 

服务工作正常。因此,测试没有。这是我的测试:

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); 
      })) 
     } 
    ) 
}); 

我一直在尝试多个小时,并阅读了很多问题和文档,但仍然无法找出它为什么不起作用。你能帮我吗?

---------------------------------------------- - - - - - - - - - 编辑 - - - - - - - - - - - - - - - - ----------------------------

我发现我的问题与coffeeScript有关。 我的控制器和服务是用coffeeScript编写的,当我启动我的测试时,我得到了与我的服务相关的语法错误。

这里是我的配置文件:

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 
    }) 
} 

我写我在JavaScript测试中,我很困惑什么我必须做,使之覆盖CoffeeScript的。

PS:我已经安装了卡玛 - 咖啡预处理

+0

“什么不工作?”,在控制台中的任何错误? –

+0

您是否在测试用例配置中添加了特定文件? – Rohit

+0

@PankajParkar:我无法处理coffeeScript控制器/服务 – AsmaG

回答

0

考虑下面的例子从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); 
    })); 
}); 

我会扣除您需要做的是这样的:

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); 
    })); 
}); 

希望这可以帮助。

+0

感谢你的回复,我实际上已经尝试过,但没有奏效。我认为有些事情我错过了或者做错了 – AsmaG

+0

您收到的实际错误信息将有助于分享。另外,如果你可以设置jsfiddle或类似的东西,这将使我们能够更好地帮助你。 – Nikola

+0

我发现问题在于我的控制器是用coffeeScript编写的,现在我的问题是我的服务在调试测试时出现语法错误,这意味着它不覆盖咖啡脚本文件。我会更新我的问题来澄清我的看法 – AsmaG