How do I test AngularJS code using Mocha?

前端 未结 2 1467
无人共我
无人共我 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:56

    If you're creating an angular service that doesn't have any dependencies and isn't necessarily angular specific, you can write your module in an angular-agnostic way, then either write a separate small angular wrapper for it, or test for the presence of angular, and conditionally create a service for it.

    Here's an example of an approach that I use to create modules that can be used both in angular, the browser, and node modules, including for mocha tests:

    (function(global) {
        //define your reusable component
        var Cheeseburger = {};
    
        if (typeof angular != 'undefined') {
            angular.module('Cheeseburger', [])
                .value('Cheeseburger', Cheeseburger);
        }
        //node module
        else if (typeof module != 'undefined' && module.exports) {
            module.exports = Cheeseburger
        }
        //perhaps you'd like to use this with a namespace in the browser
        else if (global.YourAppNamespace) {
            global.YourAppNamespace.Cheeseburger = Cheeseburger
        }
        //or maybe you just want it to be global
        else {
            global.Cheeseburger = Cheeseburger
        }
    })(this);
    
    0 讨论(0)
  • 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:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>myModule tests</title>
      <link rel="stylesheet" media="all" href="vendor/mocha.css">
    </head>
    <body>
      <div id="mocha"><p><a href=".">Index</a></p></div>
      <div id="messages"></div>
      <div id="fixtures"></div>
      <script src="vendor/mocha.js"></script>
      <script src="vendor/chai.js"></script>
      <script src="angular.min.js"></script>
      <script src="myModule.js"></script>
      <script>mocha.setup('tdd')</script>
      <script src="myModule_test.js"></script>
      <script>mocha.run();</script>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题