自从将Angular升级到2.3.1后,尝试分配给只读属性

时间:2017-01-13 13:46:33

标签: angular testing karma-jasmine

我今天早上将Angular 2.1.0项目升级到2.3.1,从那时起我的86个测试中有5个失败,出现以下错误:

  

失败:尝试分配给只读属性。           set @ webpack:///~/@angular/core/src/facade/errors.js:43:33< - src / test.ts:12465:52

这是最简单的失败测试:

/* tslint:disable:no-unused-variable */
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {ProfileComponent} from './profile.component';
import {TranslateModule, TranslateService} from 'ng2-translate';
import {FormsModule} from '@angular/forms';
import {ProfileService} from '../profile.service';
import {ProfileServiceStub} from '../../../testing/profile-service-stub';
import {TranslateServiceStub} from '../../../testing/translate-service-stub';
import {NotificationsServiceStub} from '../../../testing/notifications-service-stub';
import {NotificationsService} from 'angular2-notifications';

describe('ProfileComponent', () => {
  let component: ProfileComponent;
  let fixture: ComponentFixture<ProfileComponent>;
  const profileServiceStub = new ProfileServiceStub(5000);

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ProfileComponent],
      imports: [
        TranslateModule, FormsModule
      ],
      providers: [
        {provide: ProfileService, useValue: profileServiceStub},
        {provide: TranslateService, useClass: TranslateServiceStub},
        {provide: NotificationsService, useClass: NotificationsServiceStub}
      ]
    }).compileComponents();
  }));

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

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

我没有看到测试设置方式存在任何结构差异,所以我想这与我正在做的事情有关。

当我查看errors.js时,有罪的行看起来像这样:

set: function (message) { this._nativeError.message = message; },

我真的不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:5)

我弄清楚问题是什么。 我在errors.js文件中添加了console.log(),其中报告了错误,我获得了有关上下文的更多信息。事实上,我正在使用存根来进行那些错过特定EventEmitter的测试。这引发了错误,但错误报告中似乎存在一个错误,导致业力无法输出正确的消息。

console.log()是你的朋友