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