1

So I have class name Verify which calls SMS vendor to verify the otp. Now I am writing http tests in Laravel.

How can I write the HTTP tests so that when that route is executed mock Verify is called not the real implementation.

1
  • Can you share your class implementations for all involved classes Commented Dec 21, 2018 at 10:13

1 Answer 1

1

It's hard to give an exact answer without seeing some of your implementation, including the test and the class being tested, but one thing to check is that after you've created your mock, you need to bind it into the service container.

Here's a rough example, knowing nothing about the class or its methods:

$mock = Mockery::mock(Verify::class);
$mock->shouldReceive('verify')
    ->once()
    ->andReturn(true);

App::instance(Verify::class, $mock);

Then make sure that wherever you create and use the Verify class, you retrieve an instance out of the service container.

$verify = App::make(Verify::class);
// instead of $verify = new Verify();

At that point you should have the mock instance during your tests, and not the real class. More information on binding and the service container in general can be found in the documentation, and on Laracasts

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.