Domanda

How to use angular.mock.inject() with QUnit (and not Jasmine)?

The following code defines angular.mock.dump, but angular.mock.inject is undefined.

<!DOCTYPE html>
<html ng-app="mymodule">
  <head>
    <meta charset="utf-8">
    <script src="http://code.angularjs.org/1.2.4/angular.js"></script>
    <script src="http://code.angularjs.org/1.2.4/angular-mocks.js"></script>
    <script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
    <link href="http://code.jquery.com/qunit/qunit-1.14.0.css" rel="stylesheet"> 
    <script>
      angular.module( "mymodule", [ "ngMock" ] );
    </script>
  </head>
  <body>
    <div id="qunit"></div>
    <div id="qunit-fixture"></div>
  </body>
</html>
È stato utile?

Soluzione

I looked at Angular source code and it seems both angular.mock.inject and angular.mock.module are undefined unless using Jasmine or Mocka.

It seems the solution to the problem is to either manually create an injector for every test case or manually patch angular-mocks.js to make it testing framework agnostic (which should be the case IMHO):

--- angular-mocks.js    2014-03-19 07:12:40.194522300 +0100
+++ angular-mocks2.js   2014-03-19 07:12:32.344002500 +0100
@@ -1917,11 +1917,24 @@


-if(window.jasmine || window.mocha) {
+if ( window.jasmine || window.mocha || window.QUnit ) {

   var currentSpec = null,
       isSpecRunning = function() {
-        return currentSpec && (window.mocha || currentSpec.queue.running);
+        if ( window.mocha ) {
+          return currentSpec && true;
+        } else if ( window.jasmine ) {
+          return currentSpec && currentSpec.queue.running;
+        } else if ( window.QUnit ) {
+          return currentSpec && currentSpec.config.queue.length > 0;
+        }
       };

+  if ( window.QUnit ) {
+    var beforeEach = testStart;
+    var afterEach = testDone;
+    var modulefunction = "ngmodule";
+  } else {
+    var modulefunction = "module";
+  }

   beforeEach(function() {
@@ -1974,5 +1987,5 @@
    *        the module name and the value being what is returned.
    */
-  window.module = angular.mock.module = function() {
+  window[modulefunction] = angular.mock.module = function() {
     var moduleFns = Array.prototype.slice.call(arguments, 0);
     return isSpecRunning() ? workFn() : workFn;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top