0

我为 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 文档中复制和粘贴的,我不知道它为什么不起作用。

4

2 回答 2

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和作为参数传递给函数,并使用它们而不是. 现在似乎按预期工作。jasmineactualexpectedthis

编辑:原来那没有用,它只是默默地失败了。我现在已经纠正了。

于 2015-07-31T10:27:47.007 回答
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();
});
于 2015-07-31T09:25:48.720 回答