简体   繁体   English

如何对服务中的过滤器已进行单元测试

[英]How to unit test that a filter has been called in a service

I am having trouble asserting that my filter has been called after a promise has been returned. 我很难断言在返回诺言之后就调用了我的过滤器。

This code is called from a controller which then pulls the data to filter from a http GET service: 从控制器调用此代码,然后控制器从HTTP GET服务提取数据以进行过滤:

getPermissions(){
    return this.DataService.getPermissionsLOV().then((data) => {
        return this.$filter('chunkCollection')(data, 3);
    });
}

My test case looks like so: 我的测试用例看起来像这样:

it('should get the permissions', () => {
    spyOn(service, 'getPermissions').and.callThrough();
    spyOn(DataService, 'getPermissionsLOV').and.callFake(function(){
        var defer = this.$q.defer();
        defer.resolve(mockData);
        return defer.promise;
    });

    let resp;

    service.getPermissions().then((data) => {
        resp = data;
    });

    scope.$digest();

    expect(service.getPermissions).toHaveBeenCalled();
    expect(DataService.getPermissionsLOV).toHaveBeenCalled();
    expect(resp).toEqual(mockData);

});

The assertion expect(resp).toEqual(mockData); 断言expect(resp).toEqual(mockData); fails as the response is filtered by chunkCollection but I do not know how to test this call to $filter ? 由于响应由chunkCollection过滤而chunkCollection但是我不知道如何测试对$filter调用?

I have tested the filter itself separately so know that is working and karma tells me it is transforming the data when reporting the failure of the aforementioned assertion. 我已经单独测试了过滤器本身,因此知道它正在工作,业力告诉我在报告上述断言失败时正在转换数据。

I think your question is really how to mock a filter. 我认为您的问题确实是如何模拟过滤器。 You can just add a beforeEach and provide the filter before your test. 您可以只添加一个beforeEach并在测试之前提供过滤器。

var mockChunkCollectionFilter = jasmine.createSpy('chunkCollectionFilter');
beforeEach(module(function($provide) {
  $provide.value('chunkCollectionFilter', mockChunkCollectionFilter);
}));

it('should get the permissions', () => {
    spyOn(service, 'getPermissions').and.callThrough();
    spyOn(DataService, 'getPermissionsLOV').and.callFake(function(){
        var defer = this.$q.defer();
        defer.resolve(mockData);
        return defer.promise;
    });
    mockChunkCollectionFilter.and.callFake(function (data, chunks) {
      // Ignore chunks since that's what the test expects.
      return data;
    });

    let resp;

    service.getPermissions().then((data) => {
        resp = data;
    });

    scope.$digest();

    expect(service.getPermissions).toHaveBeenCalled();
    expect(mockChunkCollectionFilter).toHaveBeenCalled();
    expect(DataService.getPermissionsLOV).toHaveBeenCalled();
    expect(resp).toEqual(mockData);

});

mockChunkCollectionFilter works just like your other spies. mockChunkCollectionFilter工作原理与您的其他间谍一样。

Since you know what the mock response is and you also know what the filter is, you know how many rows will the filter return if the filter is working correctly. 因为您知道模拟响应是什么,并且也知道过滤器是什么,所以您知道如果过滤器正常工作,过滤器将返回多少行。 You just need to assert that the resolved collection contains the exact items and count after the filter is done. 您只需要断言已解析的集合包含确切的项目并在完成过滤后进行计数。 If the filter has worked correctly this data will match. 如果过滤器正常工作,则此数据将匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM