Error using Mockery/phpUnit in Laravel
Asked Answered
F

1

6

I'm a novice developer trying to get a test suite started for an existing laravel app but I have not experience in testing. Right now I'm just trying to get some tests built out to get some confidence and experience to write more substantial tests. I'm trying to test a relationship on a model(I realize it's not a very sensible tests) and trying to create a mocked model object to do so(I also understand it's better to do this in memory in a sqlite db but the major goal here is to test the controllers but I don't know how to deal with the authentication issue there). I have the following simple, stupid test:

public function testFoo()
{
    $lead = m::mock('Lead');

    $this->mock->shouldReceive('program')->once();

    $this->assertEquals($lead->program_id, $lead->program->id);
}

But I get the following error:

LeadTest::testFoo
BadMethodCallException: Received Mockery_0_Lead::getAttribute(), but no expectations were specified

I don't understand what this error is trying to tell me and I'm finding no help googling the issue or reading through the docs I can find.

I assume I'm not setting the expected return values but this is a pretty general test and it doesn't seem right to hard code expected return values. What am I missing here?

I'm just testing a Laravel relationship to make sure I have things set up/implemented correctly:

public function program()
{
    return $this->belongsTo('Program');
}
Fahey answered 1/6, 2016 at 19:6 Comment(1)
It might help if you can post your original function you are trying to test as well so we can get a better idea of exactly what the end result should be.Fresher
F
5

The problem was that I was missing the expected return value. It should've been something like this:

$this->mock->shouldReceive('program')->once()->andReturn(someObjectOrValue);

And the assertion should've been something like:

$this->assertEquals(someObjectOrValue, $lead->program->id);

The Mockery docs are a lot more verbose than I originally thought. http://docs.mockery.io/en/latest/reference/expectations.html

Fahey answered 1/6, 2016 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.