Michal Hojgr
Michal Hojgr

Reputation: 134

Mock a class, edit one method and use another

I am trying to test one method, which depends on the other. The first method registers a user but must check if the given username is available.

I tried something with test class

class Test {
    public function a() {
        return "ac";
    }

    public function b() {
        return $this->a();
    }
}

And I want to mock the class and edit behavoir of method "a".

I tried following

$m = new Test();

$mock = \Mockery::mock($m);
$mock->shouldReceive("a")
         ->andReturn("ad");

echo $mock->b();

But the method "a" stays unchanged.

How can I edit behavoir of the method "a" or, how do I do it elseway?

Thanks

Upvotes: 1

Views: 151

Answers (1)

Alexandre Butynski
Alexandre Butynski

Reputation: 6746

You could write a test like this one :

public function testMethodB() 
{
    $test = \Mockery::mock('Test[a]');
    $test->shouldReceive('a')->andReturn('ad');

    assertEquals('ad', $test->b());
}

I think it works !

Upvotes: 1

Related Questions