gpt4 book ai didi

angularjs - Angular 自动模拟 Controller /服务依赖项

转载 作者:行者123 更新时间:2023-12-02 11:28:32 26 4
gpt4 key购买 nike

在 Angular 单元测试中,需要大量不必要的代码来手动模拟/ stub 所有依赖项,特别是当我需要的只是一些通用模拟时。此外,当服务/ Controller 的依赖项列表发生更改时,测试会因缺少依赖项而中断。

在 C# 中,有一种方法可以重新配置 DI 容器,以便在调用 Resolve() 时自动模拟所有依赖项。我想要 Angular 中的类似功能。

我想从这里开始:

beforeEach(inject(function ($controller, $rootScope, agsRest) {
scope = $rootScope.$new();

sut = $controller('SearchController', {
$scope: scope
, map: {}
, agsRest: agsRest
, config: {}
, core: {}
, myPopup: {}
, popupFormatter: {}
});
}));

对于这样的事情:

beforeEach(inject(function ($controller, $rootScope, agsRest) {
scope = $rootScope.$new();

sut = autoMock("SearchController"); // instance of SearchController is returned, which has all dependencies mocked with sinon/jasmine/whatever
}));

有某种库/代码可以做到这一点吗?

最佳答案

我需要类似的东西并想出了这个小型实用程序库:tentacle.js 。我随时准备提出建议并提出改进 API 的请求。

关于angularjs - Angular 自动模拟 Controller /服务依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28147114/

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