简体   繁体   English

laravel测试入门

[英]Getting started with laravel testing

I'm trying to get to grips with testing and am trying to go about testing an app I'm developing. 我正在努力进行测试,并正在尝试测试我正在开发的应用程序。 Not quite TDD so not starting from a good place but I thought testing on a partially built app with working functionality would be easier (wrong!). TDD不太好,所以不是从一个好的地方开始的,但是我认为在具有工作功能的部分构建的应用程序上进行测试会更容易(错误!)。

I've created many of my controllers using Jeffrey Ways generators which also creates the test. 我已经使用Jeffrey Ways生成器创建了许多控制器,这些生成器也创建了测试。 I've then modified those controllers to enable my app to do the things I need it to. 然后,我已经修改了这些控制器,以使我的应用能够执行我需要做的事情。 I've set up phpunit, installed mockery and I've bought the book testing decoded and working my way through it. 我已经安装了phpunit,安装了嘲笑的东西,并且购买了经过解码的测试书,并按照自己的方式进行工作。 I can't though make the leap on testing a method I created ie how do I test this and more importantly what should I be refactoring - I need a kick start... 我虽然不能在测试我创建的方法上取得飞跃,即如何测试这个方法,更重要的是我应该重构什么-我需要一个启动...

So - here's my controller for a particular method that creates a book of tickets and then creates each individual ticket: 所以-这是我的特定方法的控制器,该方法创建一本票证,然后创建每张票证:

/**
 * 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.');
}

and here's a test I'm working on: 这是我正在做的测试:

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');
}

I'm not convinced I've handled the dummy user input correctly. 我不确信自己已正确处理了虚拟用户输入。 Also I don't know how I should isolate or test the part of the method that creates the tickets - should this be refactored - if so how? 另外,我不知道如何隔离或测试创建票证的方法的一部分-应该重构吗-如果可以的话? I also get error validate method not found so commented this out for the time being. 我也得到未找到的错误验证方法,因此暂时将其注释掉。

I want to start testing with a view to learning how and create better code - can anyone point me in the right direction to get me started on this method 我想开始进行测试,以学习如何创建更好的代码-任何人都可以指出正确的方向来使我开始使用这种方法

thanks 谢谢

I´m not familiar with laravel, but I dont think you need the Input::replace call. 我对laravel不熟悉,但我认为您不需要Input::replace调用。 You are in fact passing the $input into the $this->call() ... method. 实际上,您正在将$input传递到$this->call() ...方法中。

Then there is the shouldReceive method on your "Facades" which Laravel makes heavy use of. 然后,在您的“外观”上有shouldReceive方法,Laravel大量使用了该方法。

http://laravel.com/docs/testing http://laravel.com/docs/testing

So TicketAudit::shouldReceive('batchcreate')->once(); 所以TicketAudit::shouldReceive('batchcreate')->once(); could be a useful assertion. 可能是一个有用的断言。 As that is extracted already it makes sense to have a seperate test for that method. 由于已经提取了该方法,因此有必要对该方法进行单独测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM