PHPUnit setUp()和$this不适用于新对象


PHPUnit setUp() and $this not working for new objects

这很有效;

public function testReturnSuccessXmlIfActionIsCheckOrder()
{   
    $order = new Entity'Order;
    $store = new Entity'Store;
    $order->setStore($store);

这会导致一个错误;

public $order;
protected function setUp() {
  $order = new Entity'Order;
}
public function testReturnSuccessXmlIfActionIsCheckOrder()
{   
    $store = new Entity'Store;
    $order = $this->order->setStore($store);

错误为;

Fatal error: Call to a member function setStore() on a non-object

我只是想整理代码,并将设置对象移到setUp方法中。

显然,这个文件还有很多代码,但这基本上是我更改的唯一一个破坏它的地方。这里出了什么问题?

将其更改为:

protected function setUp() {
    $this->order = new Entity'Order;
}

您需要在成员变量前面处处加上$this

public $order;
protected function setUp() {
   $this->order = new Entity'Order;
}
public function testReturnSuccessXmlIfActionIsCheckOrder()
{   
    $store = new Entity'Store;
    $this->order = $this->order->setStore($store); // depends on your usecase