我正在尝试在 Karma/Jasmine 中为我的项目中的特定模块 destination-filters
编写一些单元测试。
模块减速:
angular.module('destination-filter', ['ngSanitize']);
除非我删除 ngSanitize
作为依赖项,否则我的测试会失败。据我了解,这是因为当模块实例化时,它会尝试引入该依赖项,但因为在我的 spec.js 文件中,我没有声明该模块失败。
规范文件:
describe('Destination Filter Controller', function () {
// Set the variables
var $controller;
var mockNgSanitize;
beforeEach(module('destination-filter'));
beforeEach(function() {
module(function($provide) {
$provide.value('ngSanitize', mockNgSanitize);
});
});
beforeEach(inject(function (_$controller_) {
$controller = _$controller_('DestinationFilterController');
}));
it('should expect the controller to not be null', function() {
// Check the controller is set
expect($controller).not.toBeNull();
});
});
以前,在模拟服务或函数时,$provide
方法已被证明非常有用,但我不确定我在这里使用它是否正确。我假设以这种方式使用的 $provide
不能模拟整个模块,而是模拟服务?
为了澄清,如果我从模块减速中删除 ...['ngSantize'])...
测试会正确实例化。我收到的错误是 Error: [$injector:modulerr] destination-filter
最佳答案
在测试中使用 ngSanitize 时,您可以采取三种选择:
- 将服务注入(inject)到您的测试中
- stub ngSanitize 上的方法调用
- 模拟整个 ngSanitize 服务
您选择的选项实际上取决于您的工作代码(而不是测试代码)中 ngSanitize 的使用。
无论您选择哪一种,都需要在测试中提供服务,都不需要 $provider
(这涵盖了选项 1,并且如果您只想将其提供给您的过滤器):
beforeEach(module('ngSanitize'));
beforeEach(inject(function(_ngSanitize_) { // the underscores are needed
mockNgSanitize = _ngSanitize_;
}));
此外,请确保 karma 拾取并加载所有 js 文件。您可以在 karma.conf.js
中定义它,方法是将它们添加到 files:
属性。
2。 stub 服务上的方法
我喜欢 stub ,并发现它们在编写测试时非常有用。您的测试应该只测试一件事,在您的情况下是过滤器。 stub 让您可以更好地控制测试,并允许您隔离被测试的事物。
通常是过滤器、 Controller ,任何东西都会调用许多其他东西(服务或工厂,如 $http 或 ngSanitize)。
假设您的过滤器使用 ngSanitize 的 $sanitize
来清理某些 html,您可以删除该方法以返回您定义的清理后的 html,以根据您的期望进行测试:
// in a beforeEach
spyOn(mockNgSanitize, "$sanitize").and.returnValue('<some>sanitized<html>');
mockNgSanitized.$sanitize(yourDirtyHtml);
See the jasmine docs了解更多信息。
您可能需要尝试监视正确的服务,但这应该可以正常工作。
3。模拟整个服务
我认为你不想选择这个选项,因为它会让你疯狂地弄清楚什么需要模拟,加上模拟可能会产生不切实际的期望,而且对你的用例来说并不是特别有用。如果你真的想尝试一下,那么像下面这样的东西就朝着正确的方向前进(再次 see the jasmine docs )
beforeEach(function() {
mockNgSanitize = ('ngSanitize', ['linky', '$sanitize', '$sanitizeProvider'];
});
it('mocks the ngSanitize service', function() {
expect(mockNgSanitize.linky).toBeDefined();
});
注意:在上面的所有代码中,请确保继续在描述 block 的顶部声明任何变量。
关于angularjs - AngularJS 的 Karma/Jasmine 中的模拟模块依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33890088/