laravel 4 mockery mock model relationships

veilig

say I have two models that extend from Eloquent and they relate to each other. Can I mock the relationship?

ie:

class Track extends Eloquent {
    public function courses()
    {
        return $this->hasMany('Course');
    }
}

class Course extends Eloquent {
    public function track()
    {
        return $this->belongsTo('Track');
    }
}

in MyTest, I want to create a mock of course, and return an instance of track, by calling the track property, not the track instance (I don't want the query builder)

use \Mockery as m;

class MyTest extends TestCase {
    public function setUp()
    {
        $track = new Track(array('title' => 'foo'));
        $course = m::mock('Course[track]', array('track' => $track));

        $track = $course->track  // <-- This should return my track object
    }
}
Jeff Lambert

Since track is a property and not a method, when creating the mock you will need to override the setAttribute and getAttribute methods of the model. Below is a solution that will let you set up an expectation for the property you're looking for:

$track = new Track(array('title' => 'foo'));
$course = m::mock('Course[setAttribute,getAttribute]');
// You don't really care what's returned from setAttribute
$course->shouldReceive('setAttribute');
// But tell getAttribute to return $track whenever 'track' is passed in
$course->shouldReceive('getAttribute')->with('track')->andReturn($track);

You don't need to specify the track method when mocking the Course object, unless you are also wanting to test code that relies on the query builder. If this is the case, then you can mock the track method like this:

// This is just a bare mock object that will return your track back
// whenever you ask for anything. Replace 'get' with whatever method 
// your code uses to access the relationship (e.g. 'first')
$relationship = m::mock();
$relationship->shouldReceive('get')->andReturn([ $track ]);

$course = m::mock('Course[track]');
$course->shouldReceive('track')->andReturn($relationship);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Testing - Throwing exception with Mockery

From Dev

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

From Dev

Laravel eloquent: Update A Model And its Relationships

From Dev

Laravel mockery

From Dev

Mock a class method using mockery and sinon

From Dev

Model inheritance in Laravel 4

From Dev

Laravel Controller testing with Mockery

From Dev

Laravel 4: Eloquent soft deletes and relationships

From Dev

Laravel 4: Model Relationships Not Working (sort of)?

From Dev

Laravel 4 - Chaining Many-to-Many relationships

From Dev

Mockery: how does a passive partial mock differ from the default mock?

From Dev

Laravel mock with Mockery Eloquent models

From Dev

Caching relationships eager loaded in the model (protected $with) with Laravel and Eloquent

From Dev

Creating Relationships in Neo4J model with after_save

From Dev

method does not exist on this mock object - Laravel , Mockery

From Dev

Laravel model relationships Class not found

From Dev

Laravel Category Model Relationships

From Dev

Mockery mock method inside closure

From Dev

Laravel5: How are Eloquent model relationships expressed in the database?

From Dev

Laravel accessing relationships - check if data exists from within the model

From Dev

Laravel 5.1 testing with model relationships and factories

From Dev

Laravel model relationships and model events

From Dev

How to mockery mock internal method

From Dev

Laravel 5.0 User Eloquent Model Mockery

From Dev

How do I traverse through model relationships in laravel with dot syntax

From Dev

Laravel dynamic relationships - access model attributes on eager load

From Dev

Laravel: Is it good practice to use model relationships within a view?

From Dev

Mockery and Laravel constructor injection

From Dev

Mockery & PHPUnit: method does not exist on this mock object

Related Related

  1. 1

    Laravel Testing - Throwing exception with Mockery

  2. 2

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

  3. 3

    Laravel eloquent: Update A Model And its Relationships

  4. 4

    Laravel mockery

  5. 5

    Mock a class method using mockery and sinon

  6. 6

    Model inheritance in Laravel 4

  7. 7

    Laravel Controller testing with Mockery

  8. 8

    Laravel 4: Eloquent soft deletes and relationships

  9. 9

    Laravel 4: Model Relationships Not Working (sort of)?

  10. 10

    Laravel 4 - Chaining Many-to-Many relationships

  11. 11

    Mockery: how does a passive partial mock differ from the default mock?

  12. 12

    Laravel mock with Mockery Eloquent models

  13. 13

    Caching relationships eager loaded in the model (protected $with) with Laravel and Eloquent

  14. 14

    Creating Relationships in Neo4J model with after_save

  15. 15

    method does not exist on this mock object - Laravel , Mockery

  16. 16

    Laravel model relationships Class not found

  17. 17

    Laravel Category Model Relationships

  18. 18

    Mockery mock method inside closure

  19. 19

    Laravel5: How are Eloquent model relationships expressed in the database?

  20. 20

    Laravel accessing relationships - check if data exists from within the model

  21. 21

    Laravel 5.1 testing with model relationships and factories

  22. 22

    Laravel model relationships and model events

  23. 23

    How to mockery mock internal method

  24. 24

    Laravel 5.0 User Eloquent Model Mockery

  25. 25

    How do I traverse through model relationships in laravel with dot syntax

  26. 26

    Laravel dynamic relationships - access model attributes on eager load

  27. 27

    Laravel: Is it good practice to use model relationships within a view?

  28. 28

    Mockery and Laravel constructor injection

  29. 29

    Mockery & PHPUnit: method does not exist on this mock object

HotTag

Archive