dtdvlazd56637 2014-08-26 07:13
浏览 28
已采纳

在派生类中模拟数据库

I've got Repository classes which implement a specific interface and also extend a base class for the specific database.

base class:

abstract class CouchDB
{
  protected $db;

  public function __construct(Sag $db)
  {
    $this->db = $db;
  }

  private function call_get($url) {...} //this is a wrapper for $db->get()
}

derived class:

class CouchExpression extends CouchDB implements ExpressionInterface
{
  public function __construct(Sag $db)
  {
    parent::__construct($db);
  }
}

the spec:

class CouchExpressionSpec extends ObjectBehavior
{
  public function let($db)
  {
    $db->beADoubleOf('\Sag');

    $this->beConstructedWith($db);
  }

  public function it_gets_returned_by_its_id()
  {
     $this->db->get('...')->willReturn([]);
  }
}

When running PHPSpec I get: property db not found Is there a way to fix this other than setting the property to public?

After setting it to publicI get: call to a member function willReturn() on a non-object. So I can't run ->willReturn() on $this->db

But why? When doing this in the letmethod of the test it works. But not all tests do need the same data returned...

How do I get this running?

  • 写回答

1条回答 默认 最新

  • dpojoxa5613 2014-08-26 12:38
    关注

    You can declare your double without the explicit call, like this:

    function let(\Sag $db)
    {
        $this->beConstructedWith($db);
    }
    

    Having done this, if you pass a property of the same name to your example, it will be the same instance of the double:

    function it_gets_returned_by_its_id($db)
    {
        $db->get('...')->willReturn([]);
    }
    

    Also, by convention let and example methods don't use the public keyword, they'll be public anyway and it reduces noise around your examples.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集