angular - 检查Angular 2单元测试用例中函数返回的对象类型
问题描述
我是编写角度单元测试的新手。我在一个组件中有 2 个方法。
方法一
canEnableButton(isButtonEnabled: boolean): boolean {
return isButtonEnabled || !this.isAvailable;
}
方法二
getObjectValue(): CustomObject {
if (!this.canEnableButton(this.limit)) {
return this.CustomValue;
}
}
我必须为 getObjectValue() 编写一个测试用例。测试用例应该检查函数是否会返回 CustomObject 或 undefined 类型的对象。
解决方案
首先,您需要配置/模拟您的方法中使用的变量。
this.isAvailable
和this.limit
。由于测试的结果取决于他们。
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should return CustomObject', () => {
component.limit = false;
component.isAvailable = true;
component.customValue = {} as CustomObject; // you need to mock customValue related to your CustomObject
expect(component.getObjectValue()).toEqual(component.customValue);
});
it('should return undefined', () => {
component.limit = true;
component.isAvailable = true;
component.customValue = {} as CustomObject; // you need to mock customValue related to your CustomObject
expect(component.getObjectValue()).toBeUndefined();
});
});
推荐阅读
- servicestack - 升级了 ServiceStack.OrmLite.SqlServer。现在收到有关 System.Text.Encoding.CodePages 的错误
- perl - 哈希错误的 Perl 动态哈希
- node.js - 如何制作用于测试 Microsoft Botbuilder SDK 的 turnContext?
- sql - 如何将时间戳作为 Web API 参数传递?
- python-3.x - python3:如何使用正则表达式在列表中查找和替换?
- c# - 获取 XML 节点的属性值
- html - Angular:我需要泳道 ngx-graph 中的所有节点
- android - Meteor 应用程序:将 TargetSDK 更新到 26 时,推送通知在 Android 8+ 上停止工作
- r - 是否可以在没有状态坐标的情况下创建等值线传单地图?
- apache-kafka - Kafka:使用 Apache Beam 进行一次语义配置