Javascript 单元测试并没有通过,即使它显然应该通过?
我有一个简单的功能,我想测试,但明显的结果是没有发生 我的函数是如何工作的(实际上它确实工作,只是测试不正确)Javascript 单元测试并没有通过,即使它显然应该通过?,javascript,angular,typescript,testing,karma-runner,Javascript,Angular,Typescript,Testing,Karma Runner,我有一个简单的功能,我想测试,但明显的结果是没有发生 我的函数是如何工作的(实际上它确实工作,只是测试不正确) 我将字符串传递到函数中 如果它与数组中的一个元素匹配 返回字符串 如果它与数组中的某个元素不匹配 返回字符串“default” 当我运行显示的测试时,我收到错误: 预期“默认”等于“兔子失败” 我的组件 const state = [ {name: 'failure'} ]; isStatus(current): string { for (const status
const state = [
{name: 'failure'}
];
isStatus(current): string {
for (const status of this.state) {
if (status.name === current) {
return current;
}
}
return 'default';
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [EventComponent, ConfirmationComponent],
imports: [ReactiveFormsModule, FormsModule],
providers: []
});
fixture = TestBed.createComponent(EventComponent);
component = fixture.componentInstance;
component.ngOnInit();
}));
it('should return current status if it is part of exceptional statuses', () => {
const returned = component.isState('failure');
expect(returned).toEqual('failure');
});
我的测试
const state = [
{name: 'failure'}
];
isStatus(current): string {
for (const status of this.state) {
if (status.name === current) {
return current;
}
}
return 'default';
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [EventComponent, ConfirmationComponent],
imports: [ReactiveFormsModule, FormsModule],
providers: []
});
fixture = TestBed.createComponent(EventComponent);
component = fixture.componentInstance;
component.ngOnInit();
}));
it('should return current status if it is part of exceptional statuses', () => {
const returned = component.isState('failure');
expect(returned).toEqual('failure');
});
我相信在循环中使用
const
就是问题所在。您应该在此处使用let
,因为使用const
可以防止在循环过程中重新分配值
isStatusExceptional(currentStatus): string {
for (let status of this.exceptionalRaceStatuses) {
if (status.name === currentStatus) {
return currentStatus;
}
}
return 'default';
}
似乎同意这一点。我认为在循环中使用
const
是个问题。您应该在此处使用let
,因为使用const
可以防止在循环过程中重新分配值
isStatusExceptional(currentStatus): string {
for (let status of this.exceptionalRaceStatuses) {
if (status.name === currentStatus) {
return currentStatus;
}
}
return 'default';
}
似乎同意这一点。在这种情况下,尽量不要使用for of循环。 您可以使用数组的some()方法重新编写组件,并创建纯函数 因此,不是:
isStatusExceptional(currentStatus): string {
for (const status of this.exceptionalRaceStatuses) {
if (status.name === currentStatus) {
return currentStatus;
}
}
return 'default';
}
写:
isStatusExceptional(current, erc = this.exceptionalRaceStatuses): string {
return erc.some(item => item.name === current) ? current : 'default';
}
在这种情况下,尽量不要使用for of循环。 您可以使用数组的some()方法重新编写组件,并创建纯函数 因此,不是:
isStatusExceptional(currentStatus): string {
for (const status of this.exceptionalRaceStatuses) {
if (status.name === currentStatus) {
return currentStatus;
}
}
return 'default';
}
写:
isStatusExceptional(current, erc = this.exceptionalRaceStatuses): string {
return erc.some(item => item.name === current) ? current : 'default';
}
我尝试了这个方法,但不允许对组件中的类成员使用const。您究竟在哪里定义了
例外赛车状态
?这是简略代码。ExceptionalRaceStatus是在将我的JSON拆分为异常数组和非异常数组后分配的,但与此非常相似,当然不是常量。好吧,我发现在不了解更多细节的情况下很难精确地指出确切的问题,但ExceptionalRaceStatus
的分配似乎有问题。如果它能工作,但只是在测试中不工作,那么在调用isStatusException
(不仅仅是ngOninit
)之前,您可能正在做一些不同的事情?正确。我会进一步调查。实际上,它是一个私有函数,所以测试是不必要的,我只是想练习一下。您可以在保持方法的私有性的同时测试它:component['isStatusException'](string)
。请参阅相关内容。我尝试了这一点,但不允许对组件中的类成员使用const。您究竟在哪里定义了例外赛车状态
?这是简略代码。ExceptionalRaceStatus是在将我的JSON拆分为异常数组和非异常数组后分配的,但与此非常相似,当然不是常量。好吧,我发现在不了解更多细节的情况下很难精确地指出确切的问题,但ExceptionalRaceStatus
的分配似乎有问题。如果它能工作,但只是在测试中不工作,那么在调用isStatusException
(不仅仅是ngOninit
)之前,您可能正在做一些不同的事情?正确。我会进一步调查。实际上,它是一个私有函数,所以测试是不必要的,我只是想练习一下。您可以在保持方法的私有性的同时测试它:component['isStatusException'](string)
。请参阅相关内容。我的Lint只是在此处使用“let”而不是“const”时发出抱怨,但这两者都不会导致代码在实践中失败,但在测试中仍然失败:-/Linter可以通过多种方式进行配置,但这对代码执行来说很重要。我认为Stanislav有一个更好的答案。我的Lint只是抱怨这里的“let”而不是“const”,但这两个词都不会导致代码在实践中失败,但在测试中仍然失败:-/Linter可以通过多种方式进行配置,但这对代码执行来说很重要。我认为Stanislav有一个更好的答案。你的代码,尽管它工作并且更优雅,仍然不能解决我的测试问题…你的代码,尽管它工作并且更优雅,仍然不能解决我的测试问题。。。