assertContains()預期在活動報名清單內,已經有已報名的使用者。
其中有一個@depends testReserve,這個叫做依賴測試。
依賴測試,如果有兩個測試用例,具有依賴關系,就可以使用測試依賴在兩個測試用例建立依賴關系。
這里將報名與取消報名分成兩個測試用例,讓取消報名的測試依賴于報名的測試。
執行測試
? how-to-write-a-phpunit-testcase git:(master) ? phpunit --bootstrap vendor/autoload.php tests/EventTest.php
PHPUnit 5.4.8 by Sebastian Bergmann and contributors.
..... 5 / 5 (100%)
Time: 78 ms, Memory: 10.00MB
OK (5 tests, 17 assertions)
testUnReserve()在注釋內利用@depends testReserve()標記依賴于testReserve()測試,而被依賴的測試可以當作producer,將值傳給依賴的測試testUnReserve()為consumer,通過參數接收。
這樣就能夠報名testReserve()與取消報名testUneserve()測試分開,testUneserve()會接收來自testReserve()的返回值,為一個兩個元素的數組,數組的第一個元素為,已經有人報名的對象,第二個元素為用戶對象,是已經報名的使用者。
原文轉自:http://www.jianshu.com/p/ba6829a6f3ec