为什么我的Jasmine规范认为我的Angular模块未定义 [英] Why does my Jasmine spec think my Angular module is undefined

查看:123
本文介绍了为什么我的Jasmine规范认为我的Angular模块未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的Jasmine规范认为我的Angular模块未定义?我添加了一行代码,在实际的模块代码下面设置一个布尔值为true,然后我在规范中控制它。它表示为true。我也尝试更改模块,因此它不是超范围(我的术语)或IIFE,但这没有效果。错误消息的要点是预期未定义为等于< jasmine.any(Object)>。

Why does my Jasmine spec think my Angular module is undefined? I've added a line of code setting a boolean to true below the actual module code, and then I console.log it from within the spec, and it indicates true. I've also tried changing the module so it is not an ultra-scope (my term) or IIFE, but that has no effect. The gist of the error message is Expected undefined to equal <jasmine.any(Object)>.

Spec runner:

Spec runner:

<!DOCTYPE HTML>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Angular Spec Runner</title>

  <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.4.1/jasmine_favicon.png">
  <link rel="stylesheet" type="text/css" href="lib/jasmine-2.4.1/jasmine.css">

  <script type="text/javascript" src="lib/jasmine-2.4.1/jasmine.js"></script>
  <script type="text/javascript" src="lib/jasmine-2.4.1/jasmine-html.js"></script>
  <script type="text/javascript" src="lib/jasmine-2.4.1/boot.js"></script>

  <script type="text/javascript" src="../../bower_components/angular/angular.min.js"></script>
  <script type="text/javascript" src="https://code.angularjs.org/1.5.2/angular-mocks.js"></script>

  <!-- include source files here... -->
  <script type="text/javascript" src="../services/common/common.js"></script>

  <!-- include spec files here... -->
  <script type="text/javascript" src="specs/commonSpec.js"></script>
</head>

<body>
</body>

</html>

Angular模块:

Angular module:

(function () {
    'use strict';

    var commonModule = angular.module('common', []);

    commonModule.factory('common',
        ['$q', '$rootScope', '$timeout', '$location', 'logger', 'toaster', common]);

    function common($q, $rootScope, $timeout, $location, logger, toaster) {
        var throttles = {};

        var service = {
            isPhoneNumber: isPhoneNumber
        };

        return service;

        function isPhoneNumber(phoneStr) {
            phoneStr = phoneStr.replace(/\D/g, ''); //strips parens, dots, dashes, etc.
            return phoneStr.length === 10
                    && parseInt(phoneStr, 10) >= 2000000000 //area code can't start with 0 or 1
                    && parseInt(phoneStr.slice(-7), 10) >= 2000000; //exchange can't start with 0 or 1
        };
    }
})();

window.commonModuleLoaded = true;

规格:

'use strict';

describe('common', function () {
    var common;

    beforeEach(module('common'));

    beforeEach(inject(function (_common_) {
        common = _common_;
    }));

    console.log('window.commonModuleLoaded :' + window.commonModuleLoaded);

    it('is an object', function () {
        expect(common).toEqual(jasmine.any(Object));
    })
});


推荐答案

此错误


TypeError:无法读取未定义属性'be'

TypeError: Cannot read property 'be' of undefined

清楚地说有这段代码有问题

clearly says that there's something wrong in this piece of code

expect(common).to.be.a('Object');

而不是普通服务。

这是因为代码是从使用Chai框架进行断言而不是Jasmine的规范中粘贴的,这两个API有不同的API。 Chai语法使用点分隔符,Jasmine方法是驼峰式的。

This results from the fact that the code was pasted from the spec that uses Chai framework for assertions and not Jasmine, those two have different APIs. Chai syntax uses dot separators, Jasmine methods are camel-cased.

Jasmine包含简单的API 在一个页面上,规范应该变为

Jasmine has simple API that is covered on a single page, the spec should become

expect(common).toEqual(jasmine.any(Object));

common 变量的唯一情况可能变为 undefined 默默地是常见的服务被意外覆盖以返回 undefined 。在任何其他情况下,将抛出注入错误。

The only case when common variable may become undefined silently is when common service was accidentally overridden to return undefined. In any other case injection error would be thrown.

规范中的注入错误


错误:[$ injector:unpr]未知提供者:loggerProvider< - logger< - 常见 http ://errors.angularjs.org/1.5.2/ $ injector / unpr?p0 = loggerProvider%20%3C-%20logger%20%3C-%20common

Error: [$injector:unpr] Unknown provider: loggerProvider <- logger <- common http://errors.angularjs.org/1.5.2/$injector/unpr?p0=loggerProvider%20%3C-%20logger%20%3C-%20common

是由于第三方模块文件未加载且模块未加载到 common 模块中而导致的。

results from the fact that third-party module files weren't loaded and the modules weren't loaded in common module.

解决这个问题的方法是在规范中加载依赖项:

A way to fix this is to load dependencies in spec:

beforeEach(module('loggerModuleName', 'toasterModuleName', 'common'));

但最好的办法是修复普通模块本身,因为模块应该显式地声明它的依赖关系,而不仅仅依靠父模块加载它们:

But the preferable way is to fix common module itself, because a module should explicitly state its dependencies, not just rely on parent module to load them:

var commonModule = angular.module('common', ['loggerModuleName', 'toasterModuleName']);

这篇关于为什么我的Jasmine规范认为我的Angular模块未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆