草庐IT

mocking-stubbing

全部标签

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

PhpUnit 测试 stub 方法多次返回

我正在用symfony2做一些PHPUnit测试。我在一项特定测试中遇到问题。我正在测试我类(class)之一的回应,当然一个回应是对的,一个是错的。我有一个我的数据库的模拟,并且我有一个来self的databaseRepository的方法的stub。问题是,在一个测试中,我对具有有效数组的方法进行了stub,在第二个测试中,我只想查询无效的null。我的数据库模拟://Settingupmockofdatabaserepositoryclass$this->db=$this->getMockBuilder('DatabaseRepository')->disableOriginal

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

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

PHPUnit stub : default return value from map

我在PHPUnit手册中读到,对于以下示例,方法调用doSomething('a','b','c')将返回d方法调用doSomething('e','f','g')将返回h。getMockBuilder('SomeClass')->getMock();//Createamapofargumentstoreturnvalues.$map=array(array('a','b','c','d'),array('e','f','g','h'));//Configurethestub.$stub->method('doSomething')->will($this->returnValueMa

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模拟,我想知道

php - mock 重载类缺少方法

在使用Mockery时需要一些帮助-我想重载一个类,该类是在一个方法中使用newHelperUtil()创建的。使用Mockeryoverload我可以做到这一点,但它给我留下了一个空的shell类。然后我似乎必须创建所有被调用的方法。有没有办法创建一个重载的完整模拟,然后只更改一个方法?$mock=\Mockery::mock('overload:'.HelperUtil::class);$mock->shouldReceive('content')->andReturnUsing(function(){return'differentcontent';});谢谢编辑:我想我想做的是

PHPUnit:如何保存传递给 stub 方法的参数

SomeClass类中的方法setValue()接受参数,但无法取回此参数以进行断言。我能否为方法setValue()创建一个stub,以允许保存传递给该方法的参数?classSomeClass{publicfunctionsetValue($name,$value){//dosomestuff}publicfunctiondoSomething(array$values){foreach($valuesas$name=>$value){$this->setValue($name,trim($value));}}}classTestSomeClassextendsPHPUnit_Fra

php - mock & PHPUnit : method does not exist on this mock object

你能告诉我问题出在哪里吗?我有一个包含以下测试的GeneratorTest.php文件:shouldReceive('put')->with('foo.txt','foobar')->once();$generator=newGenerator($fileMock);$generator->fire();}publicfunctiontestGeneratorDoesNotOverwriteFile(){$fileMock=\Mockery::mock('\stats\jway\File');$fileMock->shouldReceive('exists')->once()->and

php - mock 对象参数验证问题

考虑示例类(很抱歉它太复杂了,但它尽可能简洁):classRecordLookup{private$records=[13=>'foo',42=>'bar',];function__construct($id){$this->record=$this->records[$id];}publicfunctiongetRecord(){return$this->record;}}classRecordPage{publicfunctionrun(RecordLookup$id){return"Recordis".$id->getRecord();}}classApp{function__c