gpt4 book ai didi

javascript - 简单的 Angular Testing 失败($injector :unpr Unknown Provider) when I have no dependencies

转载 作者:数据小太阳 更新时间:2023-10-29 06:02:01 26 4
gpt4 key购买 nike

我得到 this error 。这与我的注入(inject)器无法解决所需的依赖关系有关,但即使我对 Angular 了解有限,我也很确定这段代码不应该依赖于任何模块。

此代码在浏览器中运行良好,但它似乎不想在我的测试中运行。我一直在关注文档中的 examples

我的 Angular 版本是 1.2.13(编辑:现在使用 1.12.15)。

这是我的代码:

var app = angular.module('app', [])

.controller('GreetingCtrl', function ($scope) {
$scope.title = "Hello World!";
$scope.message = "Test, test. One? Two?";
});

这是失败的 Jasmine 测试。

describe('app controllers', function () {
beforeEach(module('app'));

describe('GreetingCtrl', function () {
it('should says hello world', inject(function ($controller) {
var $scope = {};
$controller('GreetingCtrl', $scope);
expect($scope.title).toBe("Hello World!");
}));
});
});

我不相信它甚至没有达到运行我的测试的地步,因为它甚至在运行之前就失败了。我相信我也正确地正确连接了文件。这是我从 jasmine 测试运行器收到的错误。

Error: [$injector:unpr] http://errors.angularjs.org/1.2.13/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope (line 4569) (1)

编辑:尝试升级到 1.12.15,没有任何改变。

最佳答案

很明显,在 IRC 上聊了一会儿之后,文档可能已经过时了,这是一个可行的解决方案。我被链接到这个solution ,并相应地更正了我的测试。

describe('app controllers', function () {
var ctrl, scope;

beforeEach(module('app'));

describe('GreetingCtrl', function () {
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('GreetingCtrl', {$scope: scope});
}));

it('should says hello world', function () {
expect(scope.title).toBe("Hello World!");
});
});
});

编辑:

我最初不小心误读了文档,这里有一个更接近文档的更清晰的解决方案。

describe('app controllers', function () {
beforeEach(module('app'));

describe('GreetingCtrl', function () {
it('should says hello world', inject(function ($controller) {
var $scope = {};
// this is the line that caused me pain
$controller('GreetingCtrl', { $scope: $scope });
expect($scope.title).toBe("Hello World!");
}));
});
});

关于javascript - 简单的 Angular Testing 失败($injector :unpr Unknown Provider) when I have no dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22727269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com