来自测试方法正在使用的相同 class 的模拟方法
Mock method from the same class that tested method is using
我有以下代码:
class Foo() {
public function someMethod() {
...
if ($this->otherMethod($lorem, $ipsum)) {
...
}
...
}
}
我正在尝试测试 someMethod(),我不想测试 otherMethod(),因为它非常复杂而且我有专门的测试 - 在这里我只想模拟它并且 return 具体值。
所以我尝试:
$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;
}
}
我有以下代码:
class Foo() {
public function someMethod() {
...
if ($this->otherMethod($lorem, $ipsum)) {
...
}
...
}
}
我正在尝试测试 someMethod(),我不想测试 otherMethod(),因为它非常复杂而且我有专门的测试 - 在这里我只想模拟它并且 return 具体值。 所以我尝试:
$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;
}
}