Laravel 5模型嘲讽

导泻

我在不同的地方都看到过一些帖子,它们似乎都有相似的答案。但是,对我而言,我无法使Mockery对象正常工作。

属性模型看起来像这样

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Attribute extends Model {

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

像这样的价值模型

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

PHPUnit测试如下所示

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输出“再见”,尽管我在告诉我在模拟中返回“ hello”,但不是。有什么想法我可能做错了吗?

马卡努伊

如评论中所述:

更改return (new \App\Models\Value())->hello();return (\App::make('App\Models\Value'))->hello();

而在测试:$a = new \App\Models\Attribute();$a = App::make('App\Models\Attribute');这样Laravel将解决通过容器的依赖

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章