КОНТРОЛЬНАЯ РАБОТА

ел бы написать модульный тест для моего компонента, который использует Firestore, и я столкнулся с проблемой с насмешками над коллекцией Firebase.

SUT

export class TobjectsListComponent implements OnInit {
...
    constructor(private db: AngularFirestore) {
      this.tobjectDatabase = new TobjectDatabase(db);
    }
...
}

export class TobjectDatabase {
  /** Stream that emits whenever the data has been modified. */
  dataChange: BehaviorSubject<TObject[]> = new BehaviorSubject<TObject[]>([]);
  get data(): TObject[] { return this.dataChange.value; }

  constructor(private db: AngularFirestore) {
    this.db.collection<TObject>('tobjects').valueChanges()
      .subscribe(data => { this.dataChange.next(data); });
  }
}

КОНТРОЛЬНАЯ РАБОТА

class AngularFirestoreMock extends AngularFirestore {
  public collection<TObject>(name: string, queryFn?: QueryFn): AngularFirestoreCollection<TObject> {
    const ref = this.firestore.collection('tobjects');
    if (!queryFn) { queryFn = (ref) => ref; }
    return new AngularFirestoreCollection<TObject>(ref, queryFn(ref));
  }
}

describe('TobjectListComponent', () => {
  let component: TobjectsListComponent;
  let fixture: ComponentFixture<TobjectsListComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [MaterialModule],
      declarations: [TobjectsListComponent],
      providers: [{ "provide": AngularFirestore, "useValue": AngularFirestoreMock }],
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    })
  .compileComponents();
  }));

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

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Тестовый забег

После запуска теста я получаю сообщение об ошибкеTypeError: this.db.collection is not a function

Из информации трассировки стека я могу прочитать, что ошибка имеет источник в этой строке и символthis.db.**c**ollection<TObject>('tobjects').valueChanges()
вTobjectDatabase класс.

TypeError: this.db.collection is not a function
at new TobjectDatabase (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/src/app/tobjects-admin/tobjects-list/tobjects-list.component.ts:82:13)
at new TobjectsListComponent (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/src/app/tobjects-admin/tobjects-list/tobjects-list.component.ts:24:28)
at createClass (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/node_modules/@angular/core/@angular/core.es5.js:10933:1)
at createDirectiveInstance (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/node_modules/@angular/core/@angular/core.es5.js:10764:22)
at createViewNodes (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/node_modules/@angular/core/@angular/core.es5.js:12212:34)
at createRootView (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/node_modules/@angular/core/@angular/core.es5.js:12107:1)
at callWithDebugContext (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/node_modules/@angular/core/@angular/core.es5.js:13493:26)
at Object.debugCreateRootView [as createRootView] (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/node_modules/@angular/core/@angular/core.es5.js:12810:1)
at ComponentFactory_.webpackJsonp.../../../core/@angular/core.es5.js.,ComponentFactory_.create (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/node_modules/@angular/core/@angular/core.es5.js:9872:26)
at initComponent (http://localhost:9876/_karma_webpack_/webpack:/C:/git/volago2/node_modules/@angular/core/@angular/core/testing.es5.js:889:1)

В чем дело? Как я мог издеваться над этой коллекцией?

Ответы на вопрос(1)

Ваш ответ на вопрос