jeudi 19 décembre 2019

Mockery test trait and mock method

I try to unit-test a trait with two methods. I want to assert the result of foo which calls another method inside the trait:

<?php
trait Foo {
    public function foo() {
        return $this->anotherFoo();
    }

    public function anotherFoo() {
        return 'my value';
    }
}

/** @var MockInterface|Foo */
$mock = Mockery::mock(Foo::class);
$mock
    ->makePartial()
    ->shouldReceive('anotherFoo')
    ->once()
    ->andReturns('another value');

$this->assertEquals('another value', $mock->foo());

I get the following result when I run phpunit:

There was 1 failure:

1) XXX::testFoo
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'another value'
+'my value'

Is this generally possible like this?

Aucun commentaire:

Enregistrer un commentaire