草庐IT

mock-maker-inline

全部标签

javascript - 开 Jest (): How to mock ES6 class default import using factory parameter

模拟ES6类导入我想在我的测试文件中模拟我的ES6类导入。如果被模拟的类有多个消费者,将模拟移动到__mocks__中可能是有意义的,这样所有的测试都可以共享模拟,但在那之前我想将模拟保留在测试文件中。Jest.mock()jest.mock()可以模拟导入的模块。当传递单个参数时:jest.mock('./my-class.js');它使用在与模拟文件相邻的__mocks__文件夹中找到的模拟实现,或创建一个自动模拟。模块出厂参数jest.mock()采用第二个参数,这是一个模块工厂函数。对于使用exportdefault导出的ES6类,不清楚这个工厂函数应该返回什么。是不是:返回模

javascript - Angular js单元测试mock文档

我正在尝试测试Angular服务,它通过$document对DOM进行一些操作。服务与Jasmine。假设它只是将一些指令附加到元素。这样的服务可能看起来像(function(module){module.service('myService',['$document',function($document){this.doTheJob=function(){$document.find('body').append('');};}]);})(angular.module('my-app'));我想这样测试describe('Sampletest'function(){varmySer

phpunit Mock 返回 null 而原始方法返回字符串

我有这个文件结构:-module--phpunit.xml--blaat.php--tests---blaatTest.phpblaat.php的内容classBlaat{publicfunctiondoSomething(){return'myreturnvalue';}}测试/blaatTest.php的内容usePHPUnit\Framework\TestCase;require_once'./blaat.php';classblaatTestextendsTestCase{publicfunctiontestCanBeCreatedFromValidEmailAddress()

PHP 三进制 : Inline if statement

虽然以下代码没有任何问题,但它困扰着我,因为我知道它可能只是一行简单的代码。if(Auth::user()->id!=1){echoUser::where('owner',Auth::user()->id)->where('status',2)->count();}else{echoUser::where('status',2)->count();}我只是在构建语句时遇到了问题。如果有人可以建议,我已经尝试了几种变体:echoUser::(Auth::user()->id!=1?where('owner',Auth::user()->id)->)where('status',2)->c

php - mock :被动部分模拟与默认模拟有何不同?

在这个(非常)快速引用Mockery的最后一段中,作者解释了一些模拟的行为修饰符,它们不是默认的,但可能很有用。其中包括makePartial()调用和shouldDeferMissing()调用。这些与默认行为有何不同?当您创建模拟(Mockery::mock('myClass'))并且不添加任何方法期望时,所有方法调用都会尽我所能转到父级(即MyClass)看...这是Mockery快速引用的最后一部分。\Mockery::mock('MyClass')->makePartial()also\Mockery::mock('MyClass')->shouldDeferMissing(

Php规范。当 mock 必须返回一个对象时调用非对象的成员函数

我是phpspec的新手,正在尝试创建用于学习目的的虚拟转换器,但我坚持使用Calltoamemberfunctiononanon-objecterror这是我的代码:转换类private$supportedFormats=['xml','json'];private$handler;publicfunction__construct(ConvertHandler$handler){$this->handler=$handler;}publicfunctionconvert(array$data,$format){if(!$this->shlouldBeSupportedFormat(

PHPUnit 错误 fatal error :调用未定义的方法 Mock_Game_073a8e20::method()

我目前正在观看使用PHPUnit的指南,当涉及模拟时,我总是会收到此错误。游戏类classGame{protected$title;protected$imagePath;protected$ratings;publicfunctiongetAverageScore(){$ratings=$this->getRatings();$numRatings=count($ratings);$total=0;if($numRatings==0){returnnull;}foreach($ratingsas$rating){$total=$rating->getScore();}return$t

PHP 强制下载 PDF 文件,即使我使用的是 Content-Disposition : inline

如果可能的话,我正在尝试在浏览器中显示PDF——我知道我可以在Chrome中执行此操作,这正是我正在测试的。问题是,每次我尝试时,它都会提示下载相反。我正在使用PHPsession,所以我知道发送了一些无关的header,所以我调用了header_remove()来重置所有内容。我调用这个函数来显示PDF:id.'.pdf';//ThelocationofthePDFif(!file_exists($file)){die('ThePDFdoesnotexist.');//Somehowthefiledoesnotexist.}header_remove();//I'musingPHPs

php - 在使用 PHPUnit 进行测试时使用实现 IteratorAggregate 接口(interface)的 Mock 类时如何防止重新声明错误?

我正在编写依赖于外部类exceptionManager的单元测试。我希望能够预测此类中的一些特定函数将返回什么,所以我使用了一个模拟对象。代码非常简单:$mockExceptionManager=$this->getMock('exceptionManager');问题是,我的异常管理器实现了IteratorAggregate接口(interface),它需要一个如下所示的方法:publicfunctiongetIterator(){returnnewArrayIterator($this->exceptions);}当我运行单元测试时,出现以下错误:Fatalerror:Cannot

php - 如何 mock 模拟内部方法

目标.phpgetData();returntrue;}publicfunctiongetData(){returnarray();}}目标测试.phpshouldReceive('getData')->once();$expected=$this->exp->validate();$this->assertTrue($expected);}}结果Mockery\Exception\InvalidCountException:应调用Mockery_1_ExpWarning的方法getData()恰好1次但被调用0次。我使用Mockery作为模拟工具,例子总是关于如何用DI模拟,我想知道