Jamie Woods
Jamie Woods

Reputation: 579

Mock out function call to another function in same class

I have function that calls out to another function in the same class. I'm trying to test if that method is called in my unit tests however can't find a way to mock it.

Here is an example of the method call

public function follow($user_id, $followsfeed, $target_user_id)
    {
        $news_feeds = $this->getNewsFeeds($user_id);

    }

I want to mock the method getNewsFeed and assert it is being called when I execute follow.

What is the best way to achieve this. as I can't workout how to create an internal mocked object.

Upvotes: 2

Views: 3868

Answers (1)

Matteo
Matteo

Reputation: 39430

You can use a partial mocks, as example on your test class, you can do:

public function test_follow()
{
    $mock = Mockery::mock('App\Services\SomeService')->makePartial();
    $mock->shouldReceive('getNewsFeeds')
      ->once()
      ->with("id1","id2")
      ->andReturn($someNews);
    $mock-> follow("id1","id2"); 
}

You can also use the PHPUnit test-doubles

Hope this help

Upvotes: 4

Related Questions