Mat*_*aly 0 jasmine angularjs gulp-karma
我为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();
}));
});
});
Run Code Online (Sandbox Code Playgroud)
我刚刚在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
Run Code Online (Sandbox Code Playgroud)
我无法看到我出错的地方.有任何想法吗?它实际上工作正常,如果我拿出自定义匹配器,我还没有实际使用,但计划这样做.所以似乎匹配器是罪魁祸首,但由于它从字面上复制并粘贴在Angular文档中,所以我不知道为什么它不起作用.
最终解决了这个问题.事实证明,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}]);
}));
});
});
Run Code Online (Sandbox Code Playgroud)
基本上,你要做的就是改变this了jasmine当您添加的匹配,通过actual与expected作为参数传递给函数,使用他们,而不是this.似乎现在按预期工作.
编辑:原来这不起作用,它只是默默地失败.我现在纠正了它.
| 归档时间: |
|
| 查看次数: |
908 次 |
| 最近记录: |