如何在 QUnit(而不是 Jasmine)中使用 angular.mock.inject()
?
下面的代码定义了angular.mock.dump
,但是angular.mock.inject
是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>
最佳答案
我查看了 Angular 源代码,似乎 angular.mock.inject
和 angular.mock.module
都是未定义的,除非使用 Jasmine 或 Mocka。
问题的解决方案似乎是为每个测试用例手动创建注入(inject)器或手动修补 angular-mocks.js
以使其与测试框架无关(恕我直言,应该是这种情况) :
--- 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;
关于javascript - 使用 QUnit 测试 Angular.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22473070/