我用角7.1和茉莉花测试我的组件。我正试着写一些测试,但这个似乎对我来说太过分了。
我正在尝试测试的简化类:
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'a',
templateUrl: './a.html',
styleUrls: ['./a.scss']
}
export class A implements OnInit {
constructor(private http: HttpClient) {}
private privateState: State;
public publicState: State2;
ngOnInit() {
this.http.get(this.GetUrl()).subscribe((x) => {
this.privateState = x['whatever'];
});
}
private hiddenComplexFunction() {
this.publicState = this.privateState.something;
}
public testedFunction() {
//someComplex Code
this.hiddenComplexFunction();
}
}我试过的是:
A.['privateState']这样的私有变量,但没有运行从‘@ HttpClientTestingModule /公用/http/ TestBed’导入{ngOnInit};从‘@http/core/TestBed’导入{异步、ComponentFixture、TestBed};类AMock扩展A{ ngOnInit() { this.privateState = mockedState;})描述(‘A’,() => { let组件: AMock;let组件: ComponentFixture;beforeEach () => { TestBed.configureTestingModule({ imports: HttpClientTestingModule,声明: AMock }) .compileComponents().catch( });beforeEach(() => {beforeEach= TestBed.createComponent(AMock);component = fixture.componentInstance;});它(testedFunction应正确设置公共状态“,() => { component.testedFunction(););});
最后一点在运行ng test命令后无法读取未定义的属性“”时出现返回错误。
我不知道哪种方式是测试这个的正确方法。我知道我可以公开hiddenComplexFunction,我的所有问题都会消失,而且我也不喜欢这样一个事实:由于测试,我不得不更改访问修饰符(从私有更改为受保护的示例,就像模拟的示例中的那样,这在我看来完全是错误的)。
发布于 2020-01-03 14:40:02
你的问题有很多,所以我将从我能很容易看到的东西开始。
我们可以从您的核心组件代码开始。在testedFunction()中,调用this.hiddenComplexFunction()。您需要函数的实例版本,因此需要添加“this”。干杯。这是非常合适的,而且您也不想直接测试私有函数/私有属性。
接下来,我猜你想要这段代码
this.state = x['whatever'];若要设置privateState,而不是状态
this.privateState = x['whatever'];接下来,您不需要用您的AMock类来模拟A,测试操纵将为您完成。先试试下面这样的东西。我在这里没有的地方是州代码。我真正做的就是摆脱您的AMock,用实际的组件替换它,并构建一个测试状态(我可能会添加一个构建不良的状态)。哈!)。我处理下面的HTTP数据,通过查看代码,这些数据将与您的状态相关。
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { A } from './zing.component'; // this should point to your component
// get rid of this, not needed
// class AMock extends A {
// ngOnInit() {
// //privateState is now protected
// let privateState = mockedState;
// }
// }
describe('A', () => {
let component: A; // changed from AMock to A
let fixture: ComponentFixture<A>; // changed from AMock to A
let mockedState = {something: 'yelp'}; // build your expected state here
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
declarations: [A] // changed from AMock to A
})
.compileComponents().catch();
}));
beforeEach(() => {
fixture = TestBed.createComponent(A); // changed from AMock to A
component = fixture.componentInstance;
});
it('testedFunction should set up public state correctly', () => {
component.testedFunction();
expect(component.publicState).toEqual(mockedState.something);
});
});最后,对于HTTP模拟,可以使用HttpTestingController将一些数据刷新到组件中。
在描述块-
let backend: HttpTestingController;在每个人之前-
backend = TestBed.get(HttpTestingController);在你的IT声明中-
backend.expectOne(expectedURL).flush(mockedState);
backend.verify();让我们知道这对你有多大帮助,如果你有其他错误的话。
https://stackoverflow.com/questions/59579039
复制相似问题