Php 来自测试方法正在使用的同一类的Mock方法
我有以下代码:Php 来自测试方法正在使用的同一类的Mock方法,php,unit-testing,testing,codeception,mockery,Php,Unit Testing,Testing,Codeception,Mockery,我有以下代码: class Foo() { public function someMethod() { ... if ($this->otherMethod($lorem, $ipsum)) { ... } ... } } 我正在尝试测试someMethod(),我不想测试otherMethod(),因为它非常复杂,我有专门的测试——这里我只想模拟它并返回特定的值。 所以我试着: $
class Foo() {
public function someMethod() {
...
if ($this->otherMethod($lorem, $ipsum)) {
...
}
...
}
}
我正在尝试测试someMethod(),我不想测试otherMethod(),因为它非常复杂,我有专门的测试——这里我只想模拟它并返回特定的值。
所以我试着:
$fooMock = Mockery::mock(Foo::class)
->makePartial();
$fooMock->shouldReceive('otherMethod')
->withAnyArgs()
->andReturn($otherMethodReturnValue);
在测试中我打电话给你
$fooMock->someMethod()
但它使用的是原始(不是模拟的)方法otherMethod()并打印错误
Argument 1 passed to Mockery_3_Foo::otherMethod() must be an instance of SomeClass, boolean given
您能帮我吗?将此用作模拟方法的模板:
<?php
class FooTest extends \Codeception\TestCase\Test{
/**
* @test
* it should give Joy
*/
public function itShouldGiveJoy(){
//Mock otherMethod:
$fooMock = Mockery::mock(Foo::class)
->makePartial();
$mockedValue = TRUE;
$fooMock->shouldReceive('otherMethod')
->withAnyArgs()
->andReturn($mockedValue);
$returnedValue = $fooMock->someMethod();
$this->assertEquals('JOY!', $returnedValue);
$this->assertNotEquals('BOO!', $returnedValue);
}
}
class Foo{
public function someMethod() {
if($this->otherMethod()) {
return "JOY!";
}
return "BOO!";
}
public function otherMethod(){
//In the test, this method is going to get mocked to return TRUE.
//that is because this method ISN'T BUILT YET.
return false;
}
}
它会打印什么错误?打印错误是因为我正在使用的mock没有准备好用于此方法,我想模拟此方法,因为我正在单独测试它。erorr是不相关的-主要问题是模拟测试过程中使用的测试方法的服务中的一个方法,我很确定这就是我在最初的帖子中所做和描述的,但它不起作用。我不得不以肮脏的方式处理它,并开始做下一步的任务。无论如何,谢谢你的帮助,我会尽快回到这项任务,再试一次。太好了!我猜对你来说是发生在“…”部分的某个地方。您的体系结构基本上是正确的。如果你同意,就投票表决这个答案。:)