Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular <;spyOn>;:找不到要监视的对象_Angular_Karma Runner_Karma Jasmine - Fatal编程技术网

Angular <;spyOn>;:找不到要监视的对象

Angular <;spyOn>;:找不到要监视的对象,angular,karma-runner,karma-jasmine,Angular,Karma Runner,Karma Jasmine,我有一个简单的组件: .html: <h1> {{title}} </h1> <button (click)="changeTitle()">Change title</button> 规格: 前3个测试通过,最后一个测试返回Error::找不到用于监视changeTitle()的对象。 有什么问题吗?通过更新修复: spyOn(component, 'changeTitle').and.callThrough(); 致: 你能解释一下你

我有一个简单的组件:

.html:

<h1>
  {{title}}
</h1>

<button (click)="changeTitle()">Change title</button>
规格:

前3个测试通过,最后一个测试返回
Error::找不到用于监视changeTitle()的对象。

有什么问题吗?

通过更新修复:

spyOn(component, 'changeTitle').and.callThrough();
致:


你能解释一下你为什么这样做吗?它实际上是在监视你的方法吗?不管是谁否决了答案,你能解释一下原因吗?你有什么担心吗?你有更好的解决办法吗?Jasmine网站上的文件说:当没有间谍功能时,Jasmine.createSpy可以创建一个“裸”间谍。此间谍与任何其他间谍跟踪调用、参数等一样,但其背后没有实现。间谍是JavaScript对象,可以这样使用。在你的情况下,你确实有一个可以监视的功能。我非常讨厌这样的问题,因为@JanatbekSharsheyev的所有顾虑都是我来这里要解决的问题@JanatbekSharsheyev的评论解释了为什么这不是一个答案。那个测试只是确保你创建了一个无关的模拟间谍来调用,而不是你调用了你的代码。我不明白为什么这个答案会被接受。这可能是一个解决办法,但不是我们正在寻找的答案。为什么我要创造一个赤裸裸的间谍?在我的例子中,我也有一个方法,我知道它的实现@JanatbekOrozaly,我没有否决任何答案,但我想对这个问题有一个正确的答案。请帮忙。我并没有全部阅读,但这里似乎也回答了类似的问题:
import {TestBed, async} from '@angular/core/testing';

import { AppComponent } from './app.component';

describe('AppComponent', () => {
  let fixture;
  let component;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    }).compileComponents().then(() => {
      fixture = TestBed.createComponent(AppComponent);
      component = fixture.componentInsance;
    });
  }));

  it('should create the app', async(() => {
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app works!'`, async(() => {
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app works!');
  }));

  it('should render title in a h1 tag', async(() => {
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('app works!');
  }));

  it('should change the title to `New Title!`', async(() => {
    fixture.detectChanges();
    spyOn(component, 'changeTitle').and.callThrough();
    const compiled = fixture.debugElement.nativeElement;

    const button = compiled.querySelector('button');
    button.click();
    return fixture.whenStable().then(() => {
      fixture.detectChanges();
      expect(compiled.querySelector('h1').textContent).toBe('New Title!');
    });
  }));

});
spyOn(component, 'changeTitle').and.callThrough();
jasmine.createSpy('changeTitle').and.callThrough();