2013-11-14 76 views
1

我正試圖抓住測試,並試圖去測試我正在開發的應用程序。由於TDD不是很好,所以不能從一個好的地方開始,但我認爲在部分構建的具有工作功能的應用程序上測試會更容易(錯誤!)。laravel測試入門

我創建了很多使用Jeffrey Ways生成器的控制器,它們也創建了測試。然後,我修改了這些控制器,使我的應用能夠完成我需要的功能。我設置了phpunit,安裝了嘲諷,我已經購買了測試解碼的書,並以我的方式工作。我不能通過測試我創建的方法來實現飛躍,即如何測試這個,更重要的是我應該重構什麼 - 我需要一個踢開始...

所以 - 這裏是我的控制器的特定方法創建一個預訂門票,然後創建的每個單獨的門票:

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

我不相信我所處理的虛擬用戶輸入正確。此外,我不知道如何隔離或測試創建票據的方法的部分 - 是否應該重構 - 如果是這樣的話?我也得到錯誤驗證方法沒有找到,所以暫時評論這一點。

我要開始以期測試,學習如何創造更好的代碼 - 任何人都可以點我在正確的方向,讓我開始對這個方法

感謝

回答

0

我可不是熟悉laravel,但我不認爲你需要撥打Input::replace。實際上,您將$input傳遞給$this->call() ...方法。

再有就是你的「門面」,這Laravel大量使用的shouldReceive方法。

http://laravel.com/docs/testing

所以TicketAudit::shouldReceive('batchcreate')->once();可能是一個有用的斷言。 由於已經提取,因此對該方法進行單獨測試是有意義的。

+0

謝謝 - 這工作 - 我還發現了factoryMuff,它有助於其他測試的負載。謝謝 – Ray