1

내 angularJS 프로젝트에서 내 서비스 테스트를 작성하는 데 몇 가지 어려움이 있습니다. 유닛 테스트에 카르마와 재스민을 사용하고 있습니다. 시작하려면 종속성이없는 서비스를 선택했지만 테스트를 통과하지 못했습니다.서비스/공장 테스트 - AngularJS - 재스민

여기 (커피 스크립트 작성) 내 서비스

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

:

여기에 내 구성 파일입니다.

시 : 내가 설치 한 this tutorial에서 다음과 같은 예를 감안할 때 카르마 - 커피 전처리

+0

"무엇이 작동하지 않습니까?", 콘솔의 모든 오류가 있습니까? –

+0

테스트 케이스 설정에 특정 파일을 추가 했습니까? – Rohit

+0

@PankajParkar : coffeeScript 컨트롤러/서비스를 처리 할 수 ​​없습니다. – AsmaG

답변

0

:

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

:

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

난 당신이 뭔가를 할 필요가 공제 할 것 희망이 도움이됩니다.

+0

답장을 보내 주셔서 감사합니다. 실제로 이것을 시도했지만 제대로 작동하지 않았습니다. 내가 누락되거나 잘못되어있는 것이 있다고 생각합니다. – AsmaG

+0

실제 오류 메시지가 나타나면 공유하는 것이 도움이됩니다. 또한 jsfiddle 또는 그와 비슷한 것을 설정할 수 있다면 우리를 더 잘 도와 줄 수 있습니다. – Nikola

+0

문제는 내 컨트롤러가 coffeeScript로 작성되었다는 것을 알았습니다. 내 문제는 이제 테스트를 디버깅하는 동안 내 서비스의 구문 오류가 있다는 것입니다. 즉, 테스트하지 않으면 coffeescript 파일을 덮지 않습니다. 나는 나의 의지를 명확히하기 위해 나의 질문을 업데이트 할 것이다. – AsmaG