{ 完成(); }); }); });,javascript,html,angularjs,typescript,jasmine,Javascript,Html,Angularjs,Typescript,Jasmine" /> { 完成(); }); }); });,javascript,html,angularjs,typescript,jasmine,Javascript,Html,Angularjs,Typescript,Jasmine" />

Javascript 尝试为每一行编写测试用例 编写了跳跃法的测试用例 但当我看到代码覆盖率报告时,它并没有进入onloadend方法seat.onloadend 在createSpyObj中,我调用了loadend,但它仍然没有进入内部 你们能告诉我怎么修吗 下面提供我的代码和测试用例 我试图为每一行编写测试用例 跳转(inputValue:any):无效{ var=这个; var file:file=inputValue.files[0]; var seat:FileReader=newfilereader(); seat.onloadend=(e)=>{ this.encodeBase64=seat.result; that.fileSelect=$(“#laptop”).val().replace(/^.*\\/,”); 如果(that.fileSelect==“”){ 那。dragDrop=那。游泳; }否则{ 那个。dragDrop=“”; that.dragDrop=that.fileSelect; } } $('.running').show(); 如果(inputValue.files.length>0){ var wholeQuantity=0; wholeQuantity=inputValue.files[0]。大小/1048576;//大小以mb为单位 如果(总数量>5){ $('.stars').show(); $(“#笔记本电脑”).val(“”); this.fileSelect=“”; } seat.readAsDataURL(文件); } } 描述('Jasmine单元测试:手持篮球管理手机',()=>{ 让我们为体育服务:体育服务; 让体育服务:体育服务; 让解码服务:解码服务; let BasketballChain组件:手篮球链; 让kickViewrains组件:kickViewrains; 让老虎:组成老虎; 让雨虎:组成老虎; 让老虎:组成老虎; 让肯多蒂格:组成老虎; 让foodkComponent:foodkCarousel; 让kendoComponent:KendoGridComponent; beforeach(异步(()=>{ jasmine.DEFAULT\u TIMEOUT\u INTERVAL=10000; TestBed.configureTestingModule({ 导入:[HttpModule,FormsModule,BrowserModule], 声明:[handBasketballChain,KendoGridComponent,ProgressCircle, kickViewrains、handLeftSliderComponent、FoodCarousel、kickmobiles], 提供商:[体育服务,解码服务,最近固定历史服务, {provide:Router,useClass:RouterModule},validationService,saveService, 链服务] }).compileComponents()。然后(()=>{ foodktigger=TestBed.createComponent(foodkCarousel); kendotiger=TestBed.createComponent(KendoGridComponent); foodkComponent=foodktiger.componentInstance; kendoComponent=kendotiger.componentistance; tiger=TestBed.createComponent(handBasketballChain); BasketballChainComponent=tiger.componentInstance; sportservice=tiger.debugElement.injector.get(sportservice); tiger.componentInstance.kickmobiles.sportservice=tiger.debugElement.injector.get(sportservice); tiger.componentInstance.kickViewrains.SPORTSService=tiger.debugElement.injector.get(SPORTSService); decodeService=tiger.debugElement.injector.get(decodeService); BasketballChainComponent.inputfoodkCarousel=foodkComponent;//jasmine.createSpy('foodkCarousel');//.andCallFake(函数(msg){returnthis}); BasketballChainComponent.kickmobiles.gridkendo=kendoComponent; })} )); 它('Read kick mobile',(done)=>{ 让callFirstTime:boolean=true; 让url= spyOn(篮球链组件、kickmobiles、SPORTSService、'getResponse')。和。 callFake(()=>{ 如果(第一次呼叫){ callFirstTime=false;//由detectChanges()调用 可观测收益([{ “mobileId”:“100”, “mobileName”:http://localhost:3000/assets/js/actualairings.json", “mobileType”:“标题”, “移动数据”:“YWZjYXJlZ2Vyamh2dmFyZWdoYnZi”, “注”:“注”, “notesId”:“100”, “elfDocID”:“100”, “url”:”http://localhost:3000/upload", “日期”:“2017年6月27日”, “addedByName”:“Kamal”, “用户ID”:“206509786”, “操作类型”:“创建” }]); } }); const fileReaderSpy=jasmine.createSpyObj('FileReader',['readAsDataURL','onload']); spyOn(窗口,'FileReader')。和.returnValue(fileReaderSpy); 篮球链组件。踢踏机。跳跃({ 文件:“Untitled-2.txt” }); var seat=newfilereader(); //座椅。空载端(e); //BasketballChainComponent.kickmobiles.jumping.onloadend()的 tiger.whenStable()然后(()=>{ 完成(); }); }); });

Javascript 尝试为每一行编写测试用例 编写了跳跃法的测试用例 但当我看到代码覆盖率报告时,它并没有进入onloadend方法seat.onloadend 在createSpyObj中,我调用了loadend,但它仍然没有进入内部 你们能告诉我怎么修吗 下面提供我的代码和测试用例 我试图为每一行编写测试用例 跳转(inputValue:any):无效{ var=这个; var file:file=inputValue.files[0]; var seat:FileReader=newfilereader(); seat.onloadend=(e)=>{ this.encodeBase64=seat.result; that.fileSelect=$(“#laptop”).val().replace(/^.*\\/,”); 如果(that.fileSelect==“”){ 那。dragDrop=那。游泳; }否则{ 那个。dragDrop=“”; that.dragDrop=that.fileSelect; } } $('.running').show(); 如果(inputValue.files.length>0){ var wholeQuantity=0; wholeQuantity=inputValue.files[0]。大小/1048576;//大小以mb为单位 如果(总数量>5){ $('.stars').show(); $(“#笔记本电脑”).val(“”); this.fileSelect=“”; } seat.readAsDataURL(文件); } } 描述('Jasmine单元测试:手持篮球管理手机',()=>{ 让我们为体育服务:体育服务; 让体育服务:体育服务; 让解码服务:解码服务; let BasketballChain组件:手篮球链; 让kickViewrains组件:kickViewrains; 让老虎:组成老虎; 让雨虎:组成老虎; 让老虎:组成老虎; 让肯多蒂格:组成老虎; 让foodkComponent:foodkCarousel; 让kendoComponent:KendoGridComponent; beforeach(异步(()=>{ jasmine.DEFAULT\u TIMEOUT\u INTERVAL=10000; TestBed.configureTestingModule({ 导入:[HttpModule,FormsModule,BrowserModule], 声明:[handBasketballChain,KendoGridComponent,ProgressCircle, kickViewrains、handLeftSliderComponent、FoodCarousel、kickmobiles], 提供商:[体育服务,解码服务,最近固定历史服务, {provide:Router,useClass:RouterModule},validationService,saveService, 链服务] }).compileComponents()。然后(()=>{ foodktigger=TestBed.createComponent(foodkCarousel); kendotiger=TestBed.createComponent(KendoGridComponent); foodkComponent=foodktiger.componentInstance; kendoComponent=kendotiger.componentistance; tiger=TestBed.createComponent(handBasketballChain); BasketballChainComponent=tiger.componentInstance; sportservice=tiger.debugElement.injector.get(sportservice); tiger.componentInstance.kickmobiles.sportservice=tiger.debugElement.injector.get(sportservice); tiger.componentInstance.kickViewrains.SPORTSService=tiger.debugElement.injector.get(SPORTSService); decodeService=tiger.debugElement.injector.get(decodeService); BasketballChainComponent.inputfoodkCarousel=foodkComponent;//jasmine.createSpy('foodkCarousel');//.andCallFake(函数(msg){returnthis}); BasketballChainComponent.kickmobiles.gridkendo=kendoComponent; })} )); 它('Read kick mobile',(done)=>{ 让callFirstTime:boolean=true; 让url= spyOn(篮球链组件、kickmobiles、SPORTSService、'getResponse')。和。 callFake(()=>{ 如果(第一次呼叫){ callFirstTime=false;//由detectChanges()调用 可观测收益([{ “mobileId”:“100”, “mobileName”:http://localhost:3000/assets/js/actualairings.json", “mobileType”:“标题”, “移动数据”:“YWZjYXJlZ2Vyamh2dmFyZWdoYnZi”, “注”:“注”, “notesId”:“100”, “elfDocID”:“100”, “url”:”http://localhost:3000/upload", “日期”:“2017年6月27日”, “addedByName”:“Kamal”, “用户ID”:“206509786”, “操作类型”:“创建” }]); } }); const fileReaderSpy=jasmine.createSpyObj('FileReader',['readAsDataURL','onload']); spyOn(窗口,'FileReader')。和.returnValue(fileReaderSpy); 篮球链组件。踢踏机。跳跃({ 文件:“Untitled-2.txt” }); var seat=newfilereader(); //座椅。空载端(e); //BasketballChainComponent.kickmobiles.jumping.onloadend()的 tiger.whenStable()然后(()=>{ 完成(); }); }); });,javascript,html,angularjs,typescript,jasmine,Javascript,Html,Angularjs,Typescript,Jasmine,记住,单元测试的关键是编写小的可测试代码单元 在调用“jumping”函数之前,您在大多数情况下都是正确的,比如stubing FileReader等等。这在很大程度上是测试依赖于另一个外部库/函数/框架的代码的方法。维基百科页面中单元测试状态的相关部分 因为有些类可能有对其他类的引用,所以测试一个类经常会溢出到测试另一个类中。一个常见的例子是依赖于数据库的类:为了测试类,测试人员经常编写与数据库交互的代码。这是一个错误,因为单元测试通常不应该超出它自己的类边界,尤其不应该跨越这样的进程/网络边

记住,单元测试的关键是编写小的可测试代码单元

在调用“jumping”函数之前,您在大多数情况下都是正确的,比如stubing FileReader等等。这在很大程度上是测试依赖于另一个外部库/函数/框架的代码的方法。维基百科页面中单元测试状态的相关部分

因为有些类可能有对其他类的引用,所以测试一个类经常会溢出到测试另一个类中。一个常见的例子是依赖于数据库的类:为了测试类,测试人员经常编写与数据库交互的代码。这是一个错误,因为单元测试通常不应该超出它自己的类边界,尤其不应该跨越这样的进程/网络边界,因为这会给单元测试套件带来不可接受的性能问题

但是,当您创建虚拟文件读取器或mock时,它从不调用“onloadend”,因为mock/stub没有实现该事件和事件系统。这意味着您的模拟不完整。威克
jumping(inputValue: any): void {
    var that = this;
    var file: File = inputValue.files[0];

    var seat: FileReader = new FileReader();
    // bind the arguments for the event handler, first arg will be 'this' of the
    // loaded named function
    // second is 'that' variable, seat is seat and the final 'e' variable is
    // implicit and shouldn't be specified.
    seat.onloadend = loaded.bind(seat, that, seat); 

    $('.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);
    }
}

loaded(that: any, seat: any, e: any): void { // now a testable named function
    this.encodeBase64 = seat.result;
    that.fileSelect = $("#laptop").val().replace(/^.*\\/, "");
    if (that.fileSelect == '') {
        that.dragDrop = that.swimming;
    } else {
        that.dragDrop = "";
        that.dragDrop = that.fileSelect;
    }
}
describe('test suite', function () {
var old$ = $;

afterEach(function () {
    $ = old$;
});

it('covers all lines and else path on if but does not actually test anything', function () {
    $ = function () {
        val: function () {
            return 'Untitled-2.txt';
        }
    }; // stub JQuery

    var seat = {
        result: 'Base64encoded'
    };
    var scope = {};
    var that = {
        swimming: false,
        dragDrop: null
    };

    BasketballChainComponent.kickmobiles.loaded.call(scope, that, seat, null);
});

it('covers all lines and on if but not else and does not actually test anything', function () {
    $ = function () {
        val: function () {
            return '';
        }
    }; // stub JQuery

    var seat = {
        result: 'Base64encoded'
    };

    var scope = {};

    var that = {
        swimming: false,
        dragDrop: null
    };

    BasketballChainComponent.kickmobiles.loaded.call(scope, that, seat, null);
});