该模拟对象上不存在方法-Laravel,Mockery [英] method does not exist on this mock object - Laravel , Mockery

查看:119
本文介绍了该模拟对象上不存在方法-Laravel,Mockery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试一个简单的类.我正在关注本教程( http://code.tutsplus.com /tutorials/testing-laravel-controllers--net-31456 ).

i'm trying to test a simple class. I'm following this tutorial( http://code.tutsplus.com/tutorials/testing-laravel-controllers--net-31456 ).

在运行测试时出现此错误:

I have this error, while running tests:

Method Mockery_0_App_Interfaces_MealTypeRepositoryInterface::getValidator() does not exist on this mock object

我正在使用存储库结构.因此,我的控制器调用存储库,并返回Eloquent的响应.

Im using repository structure. So, my controller calls repository and that returns Eloquent's response.

我在php和laravel中相对较新.几天前,我已经开始学习测试,因此,我为那条凌乱的代码感到抱歉.

I'm relatively new in php and laravel. And I've started learning to test a few days ago, so I'm sorry for that messy code.

我的测试用例:

class MealTypeControllerTest extends TestCase
{
public function setUp()
{
    parent::setUp();

    $this->mock = Mockery::mock('App\Interfaces\MealTypeRepositoryInterface');
    $this->app->instance('App\Interfaces\MealTypeRepositoryInterface' , $this->mock);
}
public function tearDown()
{
    Mockery::close();
}

public function testIndex()
{
    $this->mock
         ->shouldReceive('all')
         ->once()
         ->andReturn(['mealTypes' => (object)['id' => 1 , 'name' => 'jidlo']]);

    $this->call('GET' , 'mealType');

    $this->assertViewHas('mealTypes');
}

public function testStoreFails()
{
    $input = ['name' => 'x'];

    $this->mock
         ->shouldReceive('getValidator')
         ->once()
         ->andReturn(Mockery::mock(['fails' => true]));

    $this->mock
         ->shouldReceive('create')
         ->once()
         ->with($input);


    $this->call('POST' , 'mealType' , $input ); // this line throws the error

    $this->assertRedirectedToRoute('mealType.create');//->withErrors();

    $this->assertSessionHasErrors('name');
}

}

我的EloquentMealTypeRepository: 没什么有趣的.

class EloquentMealTypeRepository implements MealTypeRepositoryInterface
{
public function all()
{
    return MealType::all();
}

public function find($id)
{
    return MealType::find($id);
}

public function create($input)
{
    return MealType::create($input);
}

public function getValidator($input)
{
    return MealType::getValidator($input);
}
}

我雄辩的实现方式: 也没有什么真正令人困扰的.

My eloquent implementation: Nothing really interresting,too.

class MealType extends Model
{
private $validator;

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

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['name'];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = [];

public function meals()
{
    return $this->hasMany('Meal');
}

public static function getValidator($fields)
{
    return Validator::make($fields, ['name' => 'required|min:3'] );
}
}

我的MealTypeRepositoryInterface:

My MealTypeRepositoryInterface:

interface MealTypeRepositoryInterface
{
public function all();

public function find($id);

public function create($input);

public function getValidator($input);
}

最后,我的控制器:

class MealTypeController extends Controller {
protected $mealType;

public function __construct(MealType $mealType)
{   
    $this->mealType = $mealType;
}


/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    $mealTypes = $this->mealType->all();
    return View::make('mealTypes.index')->with('mealTypes' ,$mealTypes);
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    $mealType = new MealTypeEloquent;
    $action = 'MealTypeController@store';
    $method = 'POST';

    return View::make('mealTypes.create_edit', compact('mealType' , 'action' , 'method') );     
}

/**
 * Validator does not work properly in tests.
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store(Request $request)
{
    $input = ['name' => $request->input('name')];

    $mealType = new $this->mealType;

    $v = $mealType->getValidator($input);

    if( $v->passes() )
    {
        $this->mealType->create($input);
        return Redirect::to('mealType');
    }
    else
    {
        $this->errors = $v;
        return Redirect::to('mealType/create')->withErrors($v);
    }
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    return View::make('mealTypes.show' , ['mealType' => $this->mealType->find($id)]);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    $mealType = $this->mealType->find($id);
    $action = 'MealTypeController@update';
    $method = 'PATCH';
    return View::make('mealTypes.create_edit')->with(compact('mealType' , 'action' , 'method'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    $mealType = $this->mealType->find($id);
    $mealType->name = \Input::get('name');
    $mealType->save();
    return redirect('mealType');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    $this->mealType->find($id)->delete();
    return redirect('mealType');
}

}

那应该是一切.值得一提的是,该应用程序可以正常运行,只是测试被搞砸了. 有人知道,为什么会这样?我看不到TestCase方法之间的区别-testIndex和testStoreFails,为什么找到方法"all"而没有找到"getValidator". 我将感谢您提供的任何建议.

That should be everything. It's worth to say that the application works, just tests are screwed up. Does anybody know, why is that happening? I cant see a difference between methods of TestCase - testIndex and testStoreFails, why method "all" is found and "getValidator" is not. I will be thankful for any tips of advices.

推荐答案

我在控制器中发现了此错误的来源. 说错了

I have found a source of this bug in controller. calling wrong

$v = $mealType->getValidator($input);

代替正确

$v = $this->mealType->getValidator($input);

这篇关于该模拟对象上不存在方法-Laravel,Mockery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆