【发布时间】:2018-02-11 12:55:19
【问题描述】:
- 已经编写了跳转方法的测试用例,
- 但当我看到代码覆盖率报告时,它并没有进入 onloadend 方法 seat.onloadend。
- 在 createSpyObj 中我调用了 loadend,但它仍然没有进入
- 你们能告诉我如何解决它吗?
- 在下面提供我的代码和测试用例。
- 我正在尝试为每一行编写测试用例。
jumping(inputValue: any): void {
var that = this;
var file: File = inputValue.files[0];
var seat: FileReader = new FileReader();
seat.onloadend = (e) => {
this.encodeBase64 = seat.result;
that.fileSelect = $("#laptop").val().replace(/^.*\\/, "");
if (that.fileSelect == '') {
that.dragDrop = that.swimming;
} else {
that.dragDrop = "";
that.dragDrop = that.fileSelect;
}
}
$('.running').show();
if (inputValue.files.length > 0) {
var wholeQuantity = 0;
wholeQuantity = inputValue.files[0].size / 1048576; //size in mb
if (wholeQuantity > 5) {
$('.stars').show();
$("#laptop").val('');
this.fileSelect = "";
}
seat.readAsDataURL(file);
}
}
describe('Jasmine Unit Tests: hand-Basketball-Manage-mobiles', () => {
let rainSPORTSService:SPORTSService;
let SPORTSService: SPORTSService;
let decodeService: DecodeService;
let BasketballChainComponent: handBasketballChain;
let kickViewrainsComponent: kickViewrains;
let tiger: Componenttiger<handBasketballChain>;
let raintiger: Componenttiger<kickViewrains>;
let foodktiger: Componenttiger<foodkCarousel>;
let kendotiger: Componenttiger<KendoGridComponent>;
let foodkComponent:foodkCarousel;
let kendoComponent:KendoGridComponent;
beforeEach(async(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
TestBed.configureTestingModule({
imports: [HttpModule, FormsModule,BrowserModule ],
declarations:[handBasketballChain, KendoGridComponent,ProgressCircle,
kickViewrains,handLeftSliderComponent,foodkCarousel,kickmobiles],
providers:[SPORTSService,DecodeService,recentPinnedHistoryService,
{provide: Router, useClass: RouterModule}, validationService,saveService,
ChainService]
}).compileComponents().then(() =>{
foodktiger = TestBed.createComponent(foodkCarousel);
kendotiger = TestBed.createComponent(KendoGridComponent);
foodkComponent = foodktiger.componentInstance;
kendoComponent = kendotiger.componentInstance;
tiger = TestBed.createComponent(handBasketballChain);
BasketballChainComponent = tiger.componentInstance;
SPORTSService = tiger.debugElement.injector.get(SPORTSService);
tiger.componentInstance.kickmobiles.SPORTSService=tiger.debugElement.injector.get(SPORTSService);
tiger.componentInstance.kickViewrains.SPORTSService=tiger.debugElement.injector.get(SPORTSService);
decodeService = tiger.debugElement.injector.get(DecodeService);
BasketballChainComponent.inputfoodkCarousel = foodkComponent; //jasmine.createSpy('foodkCarousel');//.andCallFake(function(msg) { return this });
BasketballChainComponent.kickmobiles.gridkendo=kendoComponent;
})}
));
it('Read kick mobile', (done) => {
let callFirstTime : boolean = true;
let url=
spyOn(BasketballChainComponent.kickmobiles.SPORTSService,'getResponse').and.
callFake(() => {
if(callFirstTime) {
callFirstTime = false; // Invoked by detectChanges()
return Observable.of([{
"mobileId": "100",
"mobileName": "http://localhost:3000/assets/js/actualairings.json",
"mobileType": "TITLE",
"mobileData": "YWZjYXJlZ2Vyamh2dmFyZWdoYnZi",
"notes": "",
"notesId": "100",
"elfDocID": "100",
"url": "http://localhost:3000/upload",
"date": "06/27/2017",
"addedByName": "Kamal",
"userID": "206509786",
"operationType": "create"
}]);
}
});
const fileReaderSpy = jasmine.createSpyObj('FileReader', ['readAsDataURL', 'onloadend']);
spyOn(window, 'FileReader').and.returnValue(fileReaderSpy);
BasketballChainComponent.kickmobiles.jumping({
files: "Untitled-2.txt"
});
var seat = new FileReader();
//seat.onloadend(e);
//BasketballChainComponent.kickmobiles.jumping.onloadend()
tiger.whenStable().then(() => {
done();
});
});
});
【问题讨论】:
-
也许一个好主意是将匿名函数 ((e) =>) 提取到一个命名函数中,并通过显式调用它来测试它。这样,您将降低复杂性,并且易于测试和维护。
-
@GonzaloMatheu 你能在我的测试用例中更新吗....它太令人困惑了:(
-
@texirv 希望我的回答对您有所帮助。我根据要求从信誉良好的来源中获取信息,并为您提供了有关单元测试的更多信息以及您为什么要这样做。我还解释了代码覆盖率并不是测试的全部和结束。我为我的答案付出了很多努力。我注意到您没有接受我的回答,即使它解决了您的问题,包括如何为您的每一行代码获得 100% 的覆盖率。通过不接受我的回答,您实际上拒绝了我提供的一半赏金。以后如果别人能很好地回答你的问题,请采纳最合适的答案
-
@DanielLane 感谢您的回复...我试过了,但没有用...您能回复这个吗----->>>>>嘿,当我使用您的代码时在 Visual Studio for $ 中抛出此错误 -----> [ts] 类型“()=> void”不可分配给类型“JQueryStatic”。 [ts] 类型 '() => void' 不可分配给类型 'JQueryStatic'。类型“() => void”中缺少属性“ajax”。
-
问题是覆盖 TypeScript 中的 JQuery $ 变量。我在未经测试的示例中存根的方法没有返回正确的类型,TypeScript 正在强制执行此操作。您可以使用 Jasmine 间谍来实现相同的目的。我会为你更新答案。但是请注意,stackoverflow 上的人会在这里提供帮助。我们不是来为您工作的,您不应该在不了解代码的方式、原因和意图的情况下复制和粘贴代码。
标签: javascript html angularjs typescript jasmine