jjzjj

javascript - 如何模拟浏览器的时区?

这个问题在这里已经有了答案:Howtouseacustomtimeinbrowsertotestforclientvsservertimedifference(11个答案)关闭5年前。我想测试网站中的位置功能,为了进行此测试,我需要尝试不同的时区。我使用javascript代码获取时区,调用以下函数:varoffset=newDate().getTimezoneOffset();现在这个函数返回给我180因为我在阿根廷,我需要测试不同的时区。有人知道怎么做吗?非常感谢!!

javascript - 获取 `TypeError: jest.fn is not a function`

我正在尝试使用Jest创建以下单元测试。jest.dontMock("pointsAwardingActions.js");describe("pointsawardingactions",()=>{describe("awardpoints",()=>{it("shoulddispatchbeginajaxaction",()=>{varpointsAwardingActions=require("pointsAwardingActions.js");constmockedDispatch=jest.fn();});});});但在运行npmtest后我收到以下错误。TypeErro

javascript - 如何在 Jest 中重置手动模拟

我有一个crypto的手动模拟,如下所示://__mocks__/crypto.jsconstcrypto=jest.genMockFromModule('crypto')consttoString:Function=jest.fn(()=>{return{}.toString()})constmockStringable={toString}constupdate:Function=jest.fn(()=>mockStringable)constdeciper={update}crypto.createDecipheriv=jest.fn(()=>deciper)exportdefa

javascript - Jest : Mock ES6 Module with both default and named export

我有一个带有默认导出和命名导出的ES6模块:/**/src/dependency.js**/exportfunctionutilityFunction(){returnfalse;}exportdefaultfunctionmainFunction(){return'foo';}它被第二个ES6模块使用:/**/src/myModule.js**/importmainFunction,{utilityFunction}from'./dependency';//EDIT:Fixedsyntaxerrorincodesample//exportdefaultmyModule(){expor

javascript - 如何将模拟服务注入(inject)过滤器的单元测试?

我有一个简单的angularjs过滤器(它需要一个id并将其转换为名称字符串),它依赖于自定义服务来完成它的工作:angular.module('app').filter('idToName',function(User){returnfunction(id){varresult,user;result='';if(id){result='nonamefound';user=User.getById(id);if(user){result=user.firstName;}}returnresult;};});我想为它写一个单元测试。我希望能够将用户服务的模拟注入(inject)到测试中

testing - 转到类型错误 : struct does not implement interface

这个问题在这里已经有了答案:Structdoesnotimplementinterfaceifithasafunctionwhichparameterimplementinterface(2个回答)2年前关闭。//BEGIN:externallibrarytyperealXstruct{}typerealYstruct{}func(realX)Do()realY{returnrealY{}}//ENDtypeAstruct{amyX}typemyYinterface{}typemyXinterface{Do()myY}funcfoo(arg1myY){}funcmain(){foo(r

unit-testing - 如何测试是否调用了 defer

我有以下代码func(s*MyRepo)InsertOrder(ctxcontext.Context,orderIDstring)error{query:=`INSERTINTOorders(orderID)VALUES(?)`stmt,err:=s.db.RawDatabase().PrepareContext(ctx,query)iferr!=nil{returnerr}deferstmt.Close()_,err=stmt.ExecContext(ctx,orderID)iferr!=nil{//logerr}returnerr}对应的测试用例是funcTestMyRepo_In

unit-testing - 在 Go 单元测试中使用两个不同的模拟

我正在学习使用gomock的mockgen实用程序第一次在Go中使用mock进行单元测试。我的单元测试工作正常,除了其中一个。被测方法有两个依赖项:一个依赖于数据库,另一个依赖于它调用restapi的外部服务。数据库的模拟(mockRepo)工作正常,因为被测方法正确调用模拟而不是实际的repo代码。然而,rest客户端的mock继续调用实际的rest客户端而不是mock代码。我不知道为什么。有人可以解释原因并帮助修复吗?这是我的单元测试:funcTestService_CreateWorkspace(t*testing.T){ts:=NewTestService(t)deferts.

go - 模拟一个在 Golang Http 处理程序中调用的函数

我有一个函数需要测试,它看起来像这样:funcparmHandler(whttp.ResponseWriter,r*http.Request){...data,err=backenddb_call(r*http.Request)...return}functionbackenddb_call(r*http.Request)(data[]Data,errerror){parm:=r.URL.Query().Get(parm)//GetDatafromDBforparm...return}在这种HTTP处理程序的情况下,我无法修改parmHandler参数并添加帮助接口(interface

go - 如何模拟接口(interface)实现

我的界面.gotypeMyInterfaceinterface{fun1()stringfun2()intfun3()bool}funcFoo(miMyInterface)string{returnmi.fun1()}我的接口(interface)测试.gotypeMyInterfaceImplementationstruct{}func(miMyInterfaceImplementation)fun1()string{return"foobar"}func(miMyInterfaceImplementation)fun2()int{returnint(100)}func(miMyIn