我为 AngularJS 应用程序的服务编写了以下非常简单的测试:
describe('Services', function () {
beforeEach(function(){
this.addMatchers({
toEqualData: function(expected) {
return angular.equals(this.actual, expected);
}
});
});
beforeEach(module('myapp.services'));
describe('Album service', function(){
var Album;
beforeEach(inject(function (_Album_) {
Album = _Album_;
}));
it('can get an instance of the Album factory', inject(function (Album) {
expect(Album).toBeDefined();
}));
});
});
我刚刚按照 AngularJS 文档中的建议添加了自定义 toEqualData 匹配器。然而,这打破了测试。我正在使用 Gulp 运行测试gulp-karma
(尽管如果我直接使用 Karma 会出现同样的问题),我看到以下错误消息:
$ gulp test
[10:00:04] Using gulpfile ~/Projects/myapp/gulpfile.js
[10:00:04] Starting 'jshint'...
[10:00:04] Starting 'karma'...
WARN `start` method is deprecated since 0.13. It will be removed in 0.14. Please use
server = new Server(config, [done])
server.start()
instead.
31 07 2015 10:00:04.362:INFO [karma]: Karma v0.13.3 server started at http://localhost:9876/
31 07 2015 10:00:04.368:INFO [launcher]: Starting browser PhantomJS
[10:00:04] Finished 'jshint' after 277 ms
31 07 2015 10:00:04.631:INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket sFxRfQ6bJtqM_utNAAAA with id 27252937
PhantomJS 1.9.8 (Linux 0.0.0) Services Album service can get an instance of the Album factory FAILED
TypeError: 'undefined' is not a function (evaluating 'this.addMatchers')
at /home/matthew/Projects/myapp/tests/services.tests.js:7
PhantomJS 1.9.8 (Linux 0.0.0): Executed 7 of 7 (1 FAILED) (0.04 secs / 0.022 secs)
[10:00:04] Finished 'karma' after 558 ms
[10:00:04] Starting 'test'...
[10:00:04] Finished 'test' after 11 μs
我看不出我哪里出错了。有任何想法吗?如果我取出我实际上还没有使用但计划这样做的自定义匹配器,它实际上工作正常。所以看起来匹配器是罪魁祸首,但由于它是从 Angular 文档中复制和粘贴的,我不知道它为什么不起作用。