0

我第一次与嘲笑/嘲笑作斗争,我不确定下面的测试是否真的触及我的代码,或者只是测试我所做的模拟?此外,我意识到尽管它的名称如此,但这段代码并不适合存储库模式。我会努力解决这个问题。

班上:

<?php namespace Acme\Cart\Repositories;

class EloquentCartRepository{
    protected $model_name = 'CartModel';
    protected $model;
    public function __construct($model = null)
    {
        $this->model = is_null($model) ? new $this->model_name : $model;
    }

    public function create_visitor_cart($session_id,$type = 'main'){
        return $this->create('visitor',$session_id,$type);
    }
    protected function create($user_type = null,$user_identifier = null,$type = 'main')
    {
        if(is_null($user_identifier)) throw new \Exception('Cannot create create cart, missing user identifier');
        if(is_null($user_type)) throw new \Exception('Cannot create create cart, missing user type');
        if($user_type == 'visitor')
        {
            $this->model->user_session_id = $user_identifier;
        }
        else
        {
            $this->model->user_id = $user_identifier;
        }
        $this->model->type = $type;
        $this->model->save();
        return $this->model;
    }
}

我的测试:

/** @test */
public function create_visitor_cart_calls_internal()
{
    $model = m::mock('Models\CartModel');
    $model->shouldReceive('user_session_id')->with('sess123');
    $model->shouldReceive('type')->with('main');
    $model->shouldReceive('save')->andReturn($model);

    $repository = new EloquentCartRepository($model);
    $created_model = $repository->create_visitor_cart('sess123','main');
    $this->assertEquals('sess123',$created_model->user_session_id);
    $this->assertEquals('main',$created_model->type);
}

这是测试我的课程的正确方法吗?或者这是对嘲弄/嘲弄的不正确使用?

4

1 回答 1

0

您应该测试它是否已保存,而不是测试返回的内容。这意味着,那->save()是运行。你设定的期望->save()$model->shouldReceive('save')->andReturn($model);。这没有意义,因为代码不使用->save().

在编程中,您通常处理两种类型的方法:命令和查询。查询可以获得一些值,执行一些逻辑并返回一个值。命令可以获取一些值,与外部源(例如数据库)进行通信并且不返回任何内容。查询应该被存根(这意味着,它们不应该对它被调用多少做任何期望,而只对它返回什么做任何期望)并且应该模拟命令(这意味着,它们应该只包含关于多少(以及是否)它的期望叫做)。

->save()方法是一个命令:它与数据库通信。所以应该嘲讽。要模拟对象,请使用->once()Mockery 的方法。它设定了一个应该被调用一次的期望:

/** @test */
public function create_visitor_cart_calls_internal()
{
    $model = m::mock('Models\CartModel');
    $model->shouldReceive('save')->once();

    $repository = new EloquentCartRepository($model);
    $created_model = $repository->create_visitor_cart('sess123','main');
    $this->assertEquals('sess123',$created_model->user_session_id);
    $this->assertEquals('main',$created_model->type);
}

尽管它的名字,Mockery 默认是一个存根框架。除非您明确指定期望,否则它不会验证是否调用了方法->once()

有关更多信息,请参阅文档:https ://github.com/padraic/mockery-docs/blob/master/reference/expectations.rst

于 2014-03-25T21:04:13.660 回答