如何使用SpyOn本机JavaScript函数?例如.find()?

如何使用SpyOn本机JavaScript函数?例如.find()?,javascript,angular,unit-testing,jasmine,karma-jasmine,Javascript,Angular,Unit Testing,Jasmine,Karma Jasmine,我正在尝试模拟此.editedComponentDetailsData.find,如下所示: editButtonClicked(event) { let rowData = this.editedComponentDetailsData.find((row) => { return row.operationalSequenceNumber == this.rowKey; }) } 这不起作用: it('should do something', ()

我正在尝试模拟
此.editedComponentDetailsData.find
,如下所示:

editButtonClicked(event) {
    let rowData = this.editedComponentDetailsData.find((row) => {
      return row.operationalSequenceNumber == this.rowKey;
    })
  }
这不起作用:

 it('should do something', () => {
    let result=spyOn(subject.editedComponentDetailsData.prototype,'find').and.returnValue({prop1:''});
  });

只需为editedComponentDetailsData分配一个类似空数组的属性,然后您就可以监视find,但您只需像在组件内部一样在没有原型的情况下访问它

beforeEach(() => {
    TestBed.resetTestEnvironment();
    TestBed.initTestEnvironment(BrowserDynamicTestingModule,
      platformBrowserDynamicTesting())

    TestBed.configureTestingModule({
      declarations: [AppComponent],
      providers: [
        AppComponent
      ],

      schemas: [NO_ERRORS_SCHEMA]
    });
    fixture = TestBed.createComponent(AppComponent);
    subject = fixture.componentInstance;
    subject.editedComponentDetailsData=[]; //mock with empty array
  });
  it('should do something', () => {
    let result=spyOn(subject.editedComponentDetailsData,'find').and.returnValue({prop1:''}); //the spy
  });

只需为editedComponentDetailsData分配一个类似空数组的属性,然后您就可以监视find,但您只需像在组件内部一样在没有原型的情况下访问它

beforeEach(() => {
    TestBed.resetTestEnvironment();
    TestBed.initTestEnvironment(BrowserDynamicTestingModule,
      platformBrowserDynamicTesting())

    TestBed.configureTestingModule({
      declarations: [AppComponent],
      providers: [
        AppComponent
      ],

      schemas: [NO_ERRORS_SCHEMA]
    });
    fixture = TestBed.createComponent(AppComponent);
    subject = fixture.componentInstance;
    subject.editedComponentDetailsData=[]; //mock with empty array
  });
  it('should do something', () => {
    let result=spyOn(subject.editedComponentDetailsData,'find').and.returnValue({prop1:''}); //the spy
  });

那很容易。谢谢,很简单。谢谢