How do I test AngularJS code using Mocha?

前端 未结 2 1457
无人共我
无人共我 2021-02-05 03:25

Basically, I\'m quite experienced with Mocha (written thousands of unit tests), and I\'m quite new to AngularJS (written just my first project).

Now I am wondering how I

2条回答
  •  清酒与你
    2021-02-05 03:59

    One way of doing that is to use Angular $injector in your tests:

    myModule_test.js

    suite('myModule', function(){
      setup(function(){
        var app = angular.module('myModule', []);
        var injector = angular.injector(['myModule', 'ng']);
        var service = injector.get('myService');
      });
    
      suite('myService', function(){
        test('should return correct value', function(){
           // perform test with an instance of service here
        });
      });
    });
    

    your html should look similar to this:

    
    
    
      
      myModule tests
      
    
    
      
      

提交回复
热议问题