How to mock App::make() on UnitTesting Laravel 4

Fabrizio Fenoglio

i got a question when i was unit testing my application. I Have a method that require a dependency but only that method need it so i thought to don't inject it by construct but initialize it with App::make() of the IoC container Class. But now how can i unit test that?

Let's say a short example for understand how you unit testing this function of example

class Example {

  public function methodToTest()
  {
       $dependency = App::make('Dependency');
       return $dependency->method('toTest');
  }

}

Test

public function test_MethodToTest() {
  $dependency =  m::mock('Dependency');
  $dependency->shouldReceive('method')->once()->with('toTest')->andReturn(true);

  $class = new Example();
  $this->assertTrue($class->methodToTest('toTest')); // does not work
}
petercoles

You're almost there. Create an anonymous mock with the expectations that you need and then register that mock as the instance for Dependency and you should be good to go.

That would look something like this

public function test_MethodToTest() {
    $dependency =  m::mock();
    $dependency->shouldReceive('method')->once()->with('toTest')->andReturn(true);
    App::instance('Dependancy', $dependancy);

    $class = new Example();
    $this->assertTrue($class->methodToTest()); // should work
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unittesting Laravel 5 Mail using Mock

From Dev

Laravel 4: Confused about how to use App::make()

From Dev

Laravel 4: Confused about how to use App::make()

From Dev

Mock an Eureka Feign Client for Unittesting

From Dev

Unittesting Django CreateView with mock library

From Dev

How to make Laravel 4 pagination

From Dev

Python: Creating a mock or fake directory with files for unittesting

From Dev

how to make a sinatra app run in rails 4?

From Dev

IronMq + Laravel4: How make it working

From Dev

How to override View::make() in Laravel 4?

From Dev

IronMq + Laravel4: How make it working

From Dev

How to override View::make() in Laravel 4?

From Dev

How make mock of final class?

From Dev

how to mock the backend for angularjs app?

From Dev

How to mock Cache::remember in Laravel

From Dev

laravel 4 mockery mock model relationships

From Dev

laravel 4 mockery mock model relationships

From Dev

Confirming function calls inside a function I'm unittesting with mock module

From Dev

Laravel 4 with Ember App

From Dev

How to make a route filters base on user type in Laravel 4?

From Dev

How can I make mock.mock_open raise an IOError?

From Dev

How to make a mock object throw an exception in Google Mock?

From Dev

laravel app::make() returns an error

From Dev

jMockit - How make constructor invocation to return a mock

From Java

How to properly make mock throw an error in Jest?

From Dev

How to make a mocked method return the same mock

From Dev

How to make mock consume some time rspec

From Dev

How to make a change value on a mock with a static method?

From Dev

How to stub and mock interactive ruby app with Cucumber?

Related Related

HotTag

Archive