【问题标题】:Attempted to assign to readonly property since upgrading Angular to 2.3.1将 Angular 升级到 2.3.1 后尝试分配给只读属性
【发布时间】:2017-01-13 13:46:33
【问题描述】:

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

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

这是失败的最简单的测试:

/* 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; },

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

【问题讨论】:

  • 我也有同样的问题。不知道如何调试这个。

标签: angular testing karma-jasmine


【解决方案1】:

我发现了问题所在。 我在错误报告的errors.js文件中添加了console.log(),并获得了有关上下文的更多信息。事实上,我正在为那些错过特定 EventEmitter 的测试使用存根。这触发了一个错误,但错误报告中似乎存在一个错误,导致 karma 无法输出正确的消息。

console.log()是你的朋友

【讨论】:

    猜你喜欢
    • 2018-12-26
    • 1970-01-01
    • 2019-12-09
    • 2019-05-08
    • 1970-01-01
    • 2020-04-08
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多