#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Mocking and Stubbing in Tests

Some dependencies — payment gateways, external APIs, email providers — shouldn't actually run during a test. Mocking and stubbing let a test replace these dependencies with controlled, predictable stand-ins.

Mocking a Facade

Mail::fake();

// ... code that sends mail ...

Mail::assertSent(WelcomeEmail::class);

Mail::fake() swaps the real mailer for a fake one that records what would have been sent, without actually sending anything.

Mocking a Class Dependency

$mock = $this->mock(PaymentGateway::class, function ($mock) {
    $mock->shouldReceive('charge')->once()->andReturn(true);
});

Stubs vs Mocks

ConceptPurpose
StubReturns canned data, doesn't verify it was called
MockVerifies specific methods were called with specific arguments

Mocking removes real dependencies from a test, but tests still need realistic data to work with — which is exactly what model factories are built for.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course