出现这个问题通常是因为在 Angular 中定义类或接口的时候,没有为属性设置 getter 方法,而在测试中使用了该属性,从而导致 Jasmine 报错。
为了解决这个问题,可以在定义类或接口的时候添加 getter 方法,让 Jasmine 能够正确访问属性。示例代码如下:
// 定义一个类 export class Person { private _name: string;
constructor(name: string) { this._name = name; }
// 添加 getter 方法 get name () { return this._name; } }
// 在测试中使用该类的 name 属性 it('should get name', () => { const person = new Person('John'); expect(person.name).toEqual('John'); });
通过为属性添加 getter 方法,就可以避免出现“Property does not have access type get”的问题了。
上一篇:AngularJasmine-expect().toHaveBeenCalled()无法工作
下一篇:AngularJasmine测试-我应该测试模型构造函数Object.assign(this,values)吗?”