Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.0模型测试模拟无法模拟方法/静态方法不存在?_Php_Unit Testing_Model_Laravel 5_Mockery - Fatal编程技术网

Php Laravel 5.0模型测试模拟无法模拟方法/静态方法不存在?

Php Laravel 5.0模型测试模拟无法模拟方法/静态方法不存在?,php,unit-testing,model,laravel-5,mockery,Php,Unit Testing,Model,Laravel 5,Mockery,我正在使用Laravel5.0创建phpunit测试和实际模型。 我在phpunit测试中得到错误,但当控制器调用模型并返回所需数据时没有错误 sample.php <?php namespace App; use Illuminate\Database\Eloquent\Model; class sample extends Model { /** * The database table used by the model. * * @var string */ protec

我正在使用Laravel5.0创建phpunit测试和实际模型。 我在phpunit测试中得到错误,但当控制器调用模型并返回所需数据时没有错误

sample.php

<?php namespace App;
use Illuminate\Database\Eloquent\Model;


class sample extends Model  {


/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'sample';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['id','username','details','image'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/

public static function test()
{
    return "Returned Text.";
}
public static function gettest()
{
    return self::test();
}

public static function getItem()
{
    return self::orderBy('username','asc')->get();
}

public static function findItem($id)
{
    return self::find($id);
}

}

这不是一个PHPUnit问题,而是一个模拟问题。不需要道歉,只是想帮助你更快地得到答案:-)你为什么要模拟一个你正在测试的类?这是我在模拟中遇到问题的代码的复制。测试函数的实际代码总是返回静态数据;数组。但是,当我尝试如上所示模拟时,它仍然返回实际数据,而不是模拟的数据。这不是一个PHPUnit问题,而是一个模拟问题。无需道歉,只是想帮助您更快地得到答案:-)为什么您要模拟当前正在测试的类?这是我在模拟中遇到问题的代码的复制。测试函数的实际代码总是返回静态数据;数组。但是,当我尝试如上所示模拟时,它仍然返回实际数据,而不是模拟的数据。这不是一个PHPUnit问题,而是一个模拟问题。无需道歉,只是想帮助您更快地得到答案:-)为什么您要模拟当前正在测试的类?这是我在模拟中遇到问题的代码的复制。测试函数的实际代码总是返回静态数据;数组。但是,当我尝试如上所示模拟时,它仍然返回实际数据,而不是模拟的数据。
<?php namespace App;

use Mockery as m;

class SampleTest extends \PHPUnit_Framework_TestCase {

protected function setUp()
{
    $this->mock = m::mock('App\sample')->makePartial();
}

protected function tearDown()
{
    m::close(); 
}

/** @test */
public function should_return_string()
{
    $response = $this->mock->test();
    var_dump("test() returns :".$response);
}

/** @test */
public function should_return_string_from_test_function()
{
    $response = $this->mock->gettest();
    var_dump("gettest() returns :".$response);
}

/** @test */
public function should_return_mocked_data()
{
    $this->mock->shouldReceive('test')->andReturn('Return Mocked Data');
    $response = $this->mock->gettest();
    var_dump("gettest() returns :".$response);
}

/** @test */
public function should_return_some_data_using_this_mock()
{
    $this->mock->shouldReceive('get')->andReturn('hello');
    $response = $this->mock->getItem();
}

}