我为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();
}));
});
});
我刚添加了自定义toEqualData匹配器as advised in the AngularJS documentation。然而,这打破了测试。我使用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文档中复制和粘贴,我不知道它为什么不起作用。
答案 0 :(得分:9)
最终解决了这个问题。事实证明,Jasmine API已经在1.3和2.0之间发生了变化,AngularJS文档似乎没有提供任何相关信息。以下是我如何解决问题,以便其他任何坚持这一点的人都可以找到合适的解决方案:
describe('Services', function () {
beforeEach(function(){
jasmine.addMatchers({
toEqualData: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
return {
pass: angular.equals(actual, expected)
};
}
};
}
});
});
beforeEach(module('myapp.services'));
describe('Album service', function(){
var Album;
beforeEach(inject(function (_Album_, _$httpBackend_) {
Album = _Album_;
mockBackend = _$httpBackend_;
}));
it('can get an instance of the Album factory', inject(function (Album) {
mockBackend.expectGET('https://myapp.com/api/albums').respond([{id: 1}, {id: 2}]);
expect(Album).toBeDefined();
var albums = Album.query();
mockBackend.flush();
expect(albums).toEqualData([{id: 1}, {id: 2}]);
}));
});
});
基本上,当你添加匹配器,通过this
和jasmine
作为函数的参数时,你只需更改actual
expected
,并使用它们而不是比this
。似乎现在按预期工作。
答案 1 :(得分:1)
我相信你试图在你的测试中注入Album
,但它是一个实例。您已在_Album_
中注入beforeEach
。
尝试删除第二个inject
(it()
中的那个)。
angulardocs中的示例:
// Defined out reference variable outside
var myService;
// Wrap the parameter in underscores
beforeEach( inject( function(_myService_){
myService = _myService_;
}));
// Use myService in a series of tests.
it('makes use of myService', function() {
myService.doStuff();
});