2

I have seen a few posts in various places about this and they all seem to have a similar answer. However for the life of me I cannot get the Mockery object working properly.

The Attribute model looks like this

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Attribute extends Model {

    public function test()
    {
        return (new \App\Models\Value())->hello();
    }
}

The Value model like this

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Value extends Model
{
    public function hello()
    {
        return 'goodbye';
    }
}

The PHPUnit test looks like this

use App\Models\Attribute;

class AttributeModelTest extends TestCase
{
    public function testThing()
    {
        $mock = Mockery::mock('\App\Models\Value');

        $mock->shouldReceive('hello')
           ->once()
           ->andReturn('hello');

        $this->app->instance('\App\Models\Value', $mock);

       $a = new \App\Models\Attribute();
       $return = $a->test();
       var_dump($return);
    }
}

PHPUnit outputs 'goodbye', where I though that I am telling it to return 'hello' in the mock and it doesn't. Any ideas what I might be doing wrong?

4
  • What happens if you change $a = new \App\Models\Attribute(); with $a = App::make('App\Models\Value'); ? Commented May 26, 2015 at 17:19
  • If I do that it will basically be the same as calling $mock->hello(). Not sure if it's clear but I'm trying to mock the response that is used by Attribute. if I call $mock->hello() it returns the expected value of 'hello' Commented May 26, 2015 at 17:59
  • You are right, I got confused between both classes, I think it should work if you change return (new \App\Models\Value())->hello(); with return (App::make('App\Models\Value'))->hello(); and in the test: $a = new \App\Models\Attribute(); with $a = App::make('App\Models\Attribute'); so Laravel will resolve the dependencies through the container. Commented May 26, 2015 at 18:06
  • Yep, you're right, I thought this was the path you might be suggesting and started to follow it through but hit a bit of a wall. Your prompt made me dig a bit deeper and try something which works, I'll answer my own question unless you want to. Within the Model it's important to do \App::make('Blah') otherwise namespacing kills it Commented May 26, 2015 at 18:32

1 Answer 1

1

As discussed in comments:

Change return (new \App\Models\Value())->hello(); with return (\App::make('App\Models\Value'))->hello();

And in the test: $a = new \App\Models\Attribute(); with $a = App::make('App\Models\Attribute'); so Laravel will resolve the dependencies through the container

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.