我正在尝试掌握测试并尝试测试我正在开发的应用程序。不完全是 TDD,所以不是从一个好的地方开始,但我认为在具有工作功能的部分构建的应用程序上进行测试会更容易(错误!)。
我使用 Jeffrey Ways 生成器创建了许多控制器,这些生成器也创建了测试。然后我修改了这些控制器以使我的应用程序能够执行我需要的操作。我已经设置了 phpunit,安装了 mockery,并且我已经购买了测试解码并通过它的方式工作的书。虽然我不能在测试我创建的方法上取得飞跃,即我如何测试这个,更重要的是我应该重构什么 - 我需要一个开始......
所以 - 这是我的控制器,用于创建票簿然后创建每张票的特定方法:
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
/**
* Validation rules required:
* 1. start and end numbers must be unique
* 2. start number must be less than end number
* 3. start and end numbers must not exist in ranges created by other books eg overlap
* 4. must contain date, user
*/
{
$input = Input::all();
$validation = Validator::make($input, Book::$rules);
$input['assigned_date'] = DateTime::createFromFormat( 'd/m/Y', Input::get('assigned_date') )->format( 'Y-m-d' );
if ($validation->passes())
{
$book = $this->book->create($input);
//once created then create tickets:
TicketAudit::batchcreate($input['start_number'],$input['end_number'],$book->id);
return Redirect::route('books.index');
}
return Redirect::route('books.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
这是我正在做的一个测试:
public function testStore()
{
$input = array(
'assigned_date'=>'13/11/2013',
'start_number'=>100,
'end_number'=>200,
'assigned_owner'=>'test user'
);
Input::replace($input);
$this->mock->shouldReceive('create')->once();
//$this->validate(true);
$this->call('POST', 'books', $input);
$this->assertRedirectedToRoute('books.index');
}
我不相信我已经正确处理了虚拟用户输入。另外我不知道我应该如何隔离或测试创建票证的方法部分——这是否应该被重构——如果是这样的话怎么办?我也得到错误验证方法未找到,因此暂时将其注释掉。
我想开始测试以了解如何创建更好的代码 - 任何人都可以指出正确的方向让我开始使用这种方法
谢谢