在尝试渲染组件时,在我的项目上运行测试时,测试失败,并出现以下错误:
TypeError: testing.TestBed.inject is not a function
这是测试组件:
import { Component, Input } from "@angular/core";
@Component({
selector: 'counter',
template: `
<button (click)="decrement()">-</button>
<span data-testid="count">Current Count: {{ counter }}</span>
<button (click)="increment()">+</button>
`,
})
export class CounterComponent {
@Input() counter = 0
increment() {
this.counter += 1
}
decrement() {
this.counter -= 1
}
}
这是Angular测试库的测试:
import {render, screen } from '@testing-library/angular'
import {CounterComponent} from './prueba.component'
describe('Counter', () => {
test('should render counter', async () => {
await render(CounterComponent, {
componentProperties: {counter: 5},
})
expect(screen.getByText('Current Count: 5')).toBeTruthy();
})
})
您使用的是什么版本的@testinglibrary/angular?我认为这个版本与Angular v6不兼容。尝试升级/降级测试库。