草庐IT

unit-testing

全部标签

javascript - 使用 Sinon/Mocha 正确 stub 请求/响应

我对后端单元测试比较陌生,需要一些关于如何对以下内容进行单元测试的指导。我正在使用Mocha/Should/Sinon。exports.get=function(req,res){if(req.query.example){returnres.status(200).json({success:true});}else{returnres.status(400).json({error:true});}} 最佳答案 您可以使用Sinon的spy和stub函数来测试您的代码,如下所示:const{spy,stub}=require('

javascript - Angular Karma Jasmine - 测试功能

基本上我必须测试下面这个函数,我正在从文本文件中读取$window.resolveLocalFileSystemURL(cordova.file.dataDirectory,function(dir){varpath='somefile.txt';dir.getFile(path,{create:true},function(file){file.file(function(file){varreader=newFileReader();reader.onloadend=function(){resolve(this.result);}reader.readAsText(file);}

javascript - Node : Testing with sinon and async/await

使用sinon和async/await运行此测试时遇到问题。这是我正在做的一个例子://infilefuncsasyncfunctionfuncA(id){leturl=getRoute53()+idreturnawaitfuncB(url);}asyncfunctionfuncB(url){//emptyfunction}和测试:letfuncs=require('./funcs');...//describeletstubRoute53=null;letstubFuncB=null;letroute53='https://sample-route53.com/'letid='123

javascript - Angular 如何测试需要位置的组件

您好,感谢您抽出宝贵时间!我正在学习如何使用Angular,并且我有兴趣学习如何测试它的组件。目前我正在苦苦挣扎,因为我已经完成了Angular页面的英雄之旅教程,我正在测试代码以更好地理解它。重点是我正在测试hero-details组件,代码是:import{Component,OnInit,Input}from'@angular/core';import{ActivatedRoute}from'@angular/router';import{MyHeroService}from'../hero-service/my-hero.service';import{Location}fro

javascript - React-testing-library:因输入而改变

我正在尝试测试组件是否会因输入元素的更改而更新。我使用fireEvent.change()函数,如果我随后检查我使用getByPlaceholderText找到的节点的值,它已按预期更新。但是我看不到react组件本身的变化。这可能是因为更改直到重新渲染才会发生;我将如何测试这个?react-testing-library的rerender似乎“从头开始”启动组件(即没有新的输入值),并且waitForElement永远找不到它在等待什么。这是组件TestForm.js:importReactfrom'react';import{withState}from'recompose';co

javascript - 如何在 Internet Explorer 中触发 script.onerror?

onerrorpageonMSDN声明onerror处理程序可以附加到脚本元素,并且它“在对象加载期间发生错误时触发。”。出于单元测试的目的,我试图让这个onerror处理程序触发,但找不到合适的示例。以下代码在Firefox中触发错误,但在InternetExplorer中不显示任何警报有谁知道script.src的值会触发附加到script.onerror的处理程序? 最佳答案 我找到了这个buriedinsomeMSDNdocumentation:Notethatthedocumentationmistakenlysaysth

javascript - QUnit 测试不一致/交替失败

我有一个简化的QUnit测试,它由2个简单的测试组成,这些测试无缘无故地随机/交替地失败(它们都是原子的,这意味着一个测试不会改变另一个元素的任何内容)请参阅thisjsFiddle尝试多次运行module("BasicactionBind");//twosimpleteststest("action1",function(){ok(ele2.trigger("click").hasClass("clicked"),"basicclickaction");});test("action2",function(){ok(ele1.click().hasClass("clicked"),"

javascript - 简单的 Angular Testing 失败($injector :unpr Unknown Provider) when I have no dependencies

我得到thiserror。这与我的注入(inject)器无法解决所需的依赖关系有关,但即使我对Angular了解有限,我也很确定这段代码不应该依赖于任何模块。此代码在浏览器中运行良好,但它似乎不想在我的测试中运行。我一直在关注文档中的examples我的Angular版本是1.2.13(编辑:现在使用1.12.15)。这是我的代码:varapp=angular.module('app',[]).controller('GreetingCtrl',function($scope){$scope.title="HelloWorld!";$scope.message="Test,test.O

javascript - 测试 React Modal 组件

抱歉,我一直在尝试通过单击按钮来测试关闭我的ReactModal,这让我度过了最艰难的时光。Modal尽可能简单,我已经尝试了所有我能想到或找到的方法,但我仍然无法查询它的子项。模态组件:varReact=require('react');varModal=require('react-bootstrap').Modal;varButton=require('react-bootstrap').Button;varMyModal=React.createClass({...render:function(){return(MyModalHello,World!Close);}});我的

javascript - Jasmine : Timeout - Async callback was not invoked within timeout

我需要测试一个从url加载图像的AngularJs服务。这是我的服务:/*globalangular,Image*/(function(){'usestrict';functionSourceLoader($q,$log){/***Loadanimagefromurlandreturnapromisewhichisresolvedwhenimageisloadingisdone.*Itreturntheimagesobjectasresolvedpromise.*@paramurlsourceoftheimage*@returns{Promise}unresolvedpromiseof