Umer Abbas
Umer Abbas

Reputation: 1

Partial mocking is not working on Model - Laravel Unit test

I am trying to run this very simple unit test on laravel model class. The real implementation instead of the mock keeps being called. Can anyone help me understand what’s going wrong.

class MockTest extends \Tests\TestCase
{
    /**
     * @test
     */
    public function simpleMockTest()
    {
        $mock = Mockery::mock("App\Order[save]");
        $mock->shouldReceive('save')->once()->andReturn(5);

        $this->app->instance("App\Order", $mock);

        $order = new Order();

        $this->assertEquals(5, $order->save());

    }

}

Upvotes: 0

Views: 1051

Answers (1)

1FlyCat
1FlyCat

Reputation: 383

You're not getting the expected result because $order is not using the mocked instance of App\Order.

Using $mock instead will give you expected result:

{
    /**
     * @test
     */
    public function simpleMockTest()
    {
        $mock = Mockery::mock("App\Order")
           ->makePartial();

        $mock->shouldReceive('save')->once()->andReturn(5);

        $this->app->instance("App\Order", $mock);

        $this->assertEquals(5, $mock->save());
    }
}```



Upvotes: 1

Related Questions