<spyon>:找不到要监视的对象

时间:2017-06-27 09:17:26

标签: angular karma-runner karma-jasmine

我有一个简单的组件:

html的:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/action_bar_height"
            android:background="@color/white"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/padding"
                android:src="@drawable/logo_dark" />

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </FrameLayout>

.TS:

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

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

规格:

export class AppComponent {
  title = 'app works!';

  changeTitle() {
    this.title = 'New Title!';
  }
}

前3个测试正在通过,最后一个测试返回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!'); }); })); });

知道什么是错的吗?

1 个答案:

答案 0 :(得分:0)

通过更新修正:

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

到:

jasmine.createSpy('changeTitle').and.callThrough();