【问题标题】:When testing angularjs controller - Can't find variable: module / inject by chutzpah测试 angularjs 控制器时 - 找不到变量:模块 / 由 chutzpah 注入
【发布时间】:2014-09-17 07:04:33
【问题描述】:

我有 angularJs 控制器

angular.module('App.ctrl.guests', [])
    .controller('guestsController', ['$scope', '$http', '$location', '$timeout', 'guestsService', function ($scope, $http, $location, $timeout, guestsService) {
        $scope.tiles = [];
    }])

茉莉花测试

/// <reference path="~/Scripts/angular.js" />
/// <reference path="../../ProjectWeb/Scripts/app/guests/guestsCtrl.js" />
/// <reference path="~/Scripts/angular-mocks.js" />
/// <reference path="~/Scripts/jasmine.js" />
'use strict';
describe('App.ctrl.guests', function () {
    var scope, controller;
    beforeEach(function () {
        module('App.ctrl.guests')
    });
    beforeEach(inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        controller = $controller('guestsController', {
            $scope: scope
        });
    }));

    it('should have empty array', function () {
        expect(scope.tiles).toBe([]);
    });
})

每次我在 Visual Studio chutzpah 中运行时都会得到:

ReferenceError:找不到变量:模块在 文件:///.../JasmineTests/guestsControllerTest.js (第 5 行)

ReferenceError:找不到变量:注入 file:///.../JasmineTests/guestsControllerTest.js(第 12 行)

参考 angular.js 是否有问题,而 jasmine 不知道什么是模块/注入关键字?这是我第一次做js测试:/

【问题讨论】:

  • 您是否包含了对这些库的引用?我自己没有使用 Visual Studio,但我认为这个人正在做和你一样的设置rosher.co.uk/post/2013/10/22/…
  • 是的,我添加引用,我更新代码。
  • 谢谢gillesc,我在这篇文章中将js文件拖放到测试文件中,现在路径是正确的;)

标签: javascript angularjs visual-studio-2012 jasmine chutzpah


【解决方案1】:

几分钟前,我在我们的 TFS 构建服务器上遇到了这个确切的异常。事实证明,当“属性”对话框中的“复制到输出目录”选项设置为“不复制”之外的另一个选项时,也会发生此异常。在我们的情况下,测试了 \bin 文件夹中的几个文件,导致引用路径不正确。

【讨论】:

    【解决方案2】:

    我在this 文章中找到了关于如何拖放 JavaScript 文件进行测试和正确路径的文章。

    【讨论】:

    • 感谢分享这篇文章,它非常精确并且在第一次尝试中工作。无法感谢作者,因为 cmets 在那篇文章中已关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 2015-01-28
    • 2015-12-27
    • 2014-09-28
    相关资源
    最近更新 更多