1

我期望在我的Angular应用程序测试中未定义一个数组。为了做到这一点,我的测试需要首先调用我测试过的组件的一个函数。在该函数中,它订阅服务的函数。

这是我的测试课。包括服务和模拟服务的必要设置以及测试本身。该测试包括我已经尝试过的注释行。

let service: Service;
const mockService = new MockService();
mockService.getById = of([]);

beforeEach(async(() => {
    return TestBed.configureTestingModule({
      declarations: [leaving out for now],
      imports: [leaving out for now],
      providers: [
        {
          provide: Service,
          useValue: mockService 
        }
      ]
    })
      .compileComponents();
  }));

beforeEach(() => {
    fixture = TestBed.createComponent(Component);
    component = fixture.componentInstance;

    service = fixture.debugElement.injector.get(Service);

    fixture.detectChanges();
  });

describe('describe test', function () {
    it('test', function () {
      //spyOn(mockService, 'getItemsById');
      //spyOn(mockService, 'getItemsById').and.returnValue(of('nothing'));
      //spyOn(service, 'getItemsById');
      //spyOn(mockService, 'getItemsById').and.callThrough();
      //spyOn(service, 'getItemsById').and.callThrough();
      //spyOn(service, 'getItemsById').and.returnValue(of('nothing')); syntax error
      //spyOn(service, 'getItemsById').and.returnValue({ subscribe: () => {} }); syntax error
      //spyOn(mockService, 'getItemsById').and.returnValue({ subscribe: () => {} }); syntax error
      component.getList();

      expect(component.array).toBeUndefined();
    });
  });

我尝试的所有方法仍然导致错误:

TypeError:无法读取未定义的属性“订阅”

它引用了我订阅服务功能的组件功能中的getItemsById

我的模拟服务:

getById: Observable<any>;

constructor() {}

getItemsById(id: number){
    return this.getById;
  }

我的订阅原始服务功能的测试组件:

getList(): Model[]{
    let temp: Model[] = [];
    for (let item of this.items){
      let id = item.id;
      this.service.getItemsById(id).subscribe(
        singleIdItem => {
          temp= temp.concat(singleIdItem);
        }
      );
    }
    return temp;
  }

我没有包括我的原始服务,因为它被嘲笑,如果需要,请告诉我。我假设解决方案是某种形式,spyOn但我已经尝试了我能找到的一切。

使用 Angular 8

4

2 回答 2

1

在第一个之前试试这个beforeEach

let service: Service;
const mockService = new MockService();
mockService.getById = of([]);

我正在考虑添加mockService.getById = of([]);第二个beforeEach(现在将其从此处删除,因为它已添加到顶部)为时已晚(没有的服务getById已经注入到 的提供者中configureTestingModule)。

========================编辑===================

我会这样使用createSpyObj

let mockService: any;

beforeEach(async(() => {
      // can name service (first argument) to anything you want, but I called in service in this scenario. It is internal for you
    mockService = jasmine.createSpyObj('service', ['getItemsById']);
    TestBed.configureTestingModule({
      declarations: [leaving out for now],
      imports: [leaving out for now],
      providers: [
        {
          provide: Service,
          useValue: mockService 
        }
      ]
    })
      .compileComponents();
  }));

beforeEach(() => {
    fixture = TestBed.createComponent(Component);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

it('should do something', () => {
   mockService.getItemsById.and.returnValue(of([]));
   component.getList();
   // the rest of your assertions.
});
于 2020-02-11T14:21:06.823 回答
0

在我的测试中,我需要从我的服务中返回正确的值

describe('describe test', function () {
    it('test', function () {
      spyOn(service, 'getItemsById').and.returnValue(of([{name: 'name', id: 10, otherId: 12, startTime: 'time', endTime: 'time', description: 'desc', insertTime: 'time', insertUser:'user', updateTime: 'time', updateUser: 'user'}]));

      component.getList();

      expect(component.array.size).toEqual(1);
    });
  });

返回的值是在我的服务中的函数中Model[]定义的类型。getList

于 2020-02-12T17:59:20.003 回答