javascript - TypeError:无法读取未定义的属性(读取“和”)
问题描述
请帮忙!尝试运行浅集成测试时,我得到了上面的类型错误。
我已经多次查看我的代码以检查我是否有问题,但一切似乎都已到位。
我正在努力让这个测试通过。
expect(fixture.componentInstance.heroes.length).toBe(3)
它在 Karma 中一直因此错误而失败。
TypeError:无法读取未定义的属性(读取“和”)
import { ComponentFixture, TestBed } from "@angular/core/testing"
import { of } from "rxjs";
import { HeroService } from "../hero.service";
import { HeroesComponent } from "./heroes.component"
describe('HeroesComponent (shallow tests)', () => {
let fixture: ComponentFixture<HeroesComponent>;
let mockHeroService;
let HEROES;
beforeEach(() =>{
HEROES = [
{id:1, name: 'SpiderDude', strength: 8},
{id:2, name: 'Wonderful Woman', strength: 24},
{id:3, name: 'SuperDude', strength: 55}
];
mockHeroService = jasmine.createSpyObj(['getHeroes, addHero, deleteHero']);
TestBed.configureTestingModule({
declarations: [HeroesComponent],
providers: [
{ provide: HeroService, useValue: mockHeroService}
],
schemas: [NO_ERRORS_SCHEMA]
})
fixture = TestBed.createComponent(HeroesComponent)
})
it('should set heroes correctly from the service', () => {
mockHeroService.getHeroes.and.returnValue(of(HEROES))
fixture.detectChanges();
expect(fixture.componentInstance.heroes.length).toBe(3)
});
});
解决方案
mockHeroService 方法中的引号未正确放置。
mockHeroService = jasmine.createSpyObj(['getHeroes, addHero, deleteHero']);
应该是这个。
mockHeroService = jasmine.createSpyObj(['getHeroes', 'addHero', 'deleteHero']);
推荐阅读
- c# - Visual Studio - 更改 NuGet 的临时文件输出目录
- javascript - 全局状态在纯反应状态托管应用程序中的性能影响
- jquery - 如何禁用下拉菜单
- node.js - 如何在 Express JS 中使用 JSON?
- django - django.db.migrations.exceptions.NodeNotFoundError:迁移 admin.0004_auto_20200309_0721 依赖项引用不存在
- python - 如何通过电子邮件发送 pytest 覆盖率报告?
- javascript - 在catch中的return语句之后仍然执行代码吗?
- c# - 如何在 MDX 中实现 IN 运算符
- python - 通过递归从一个节点到另一个节点的有向图路径
- python - 如果您递归地返回一个函数会发生什么,这是一种好的做法吗?