Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using Mockery/phpUnit in Laravel

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');
}
like image 306
brianfr82 Avatar asked Jun 01 '16 19:06

brianfr82


1 Answers

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

like image 169
brianfr82 Avatar answered Nov 02 '22 05:11

brianfr82