我正在嘗試測驗是否可以將某件商品添加到我的購物清單中。我不確定我哪里出錯了,我得到的錯誤是:
ArgumentCountError : Too few arguments to function Tests\Unit\Entities\ShoppingListTest::testAddItem(), 0 passed in
這讓我感到困惑,因為我已經在需要它們的地方傳遞了引數?我是單元測驗的新手,只需要一些指導。我也意識到我可能選擇了錯誤的斷言來呼叫,所以任何輸入都會有幫助!這是我的測驗:
public function testAddItem(string $name): void
{
//Arrange: Given an item to add to the shopping list
$items = [new ShoppingItem('lettuce')];
$list = new ShoppingList('my-groceries', $items);
//Act: Add the item to the list
$list->addItem($name);
//Assert: Check to see if the item was added to the list
$this->assertContains($items, $list, 'Does not contain lettuce.');
}
這是我的購物清單類,其中包含我正在測驗的功能:
class ShoppingList implements \iterable
{
/**
* @var string
*/
private string $name;
/**
* @var ShoppingItem[]
*/
private array $items;
public function __construct(string $name, array $items)
{
$this->name = $name;
foreach($items as $item) {
if(!$item instanceof ShoppingItem) {
throw new \InvalidArgumentException("Expecting only shopping items.");
}
}
$this->items = $items;
}
public function getName(): string
{
return $this->name;
}
public function getItems(): array
{
return $this->items;
}
public function addItem(string $name): void
{
$item = new ShoppingItem($name);
$this->items[] = $item;
}
public function checkOffItem(string $name): void
{
$item = $this->findItemByName($name);
if($item) {
$item->checkOffItem();
return;
}
throw new \LogicException("There is no item on this list called $name.");
}
private function findItemByName(string $name): ?ShoppingItem
{
foreach($this->items as $item) {
if($item->getName() === $name) {
return $item;
}
}
return null;
}
uj5u.com熱心網友回復:
這是通過的測驗。我沒有在串列中添加其他專案。
public function testAddItem(): void
{
//Arrange: Given an item to add to the shopping list
$items = [new ShoppingItem('lettuce')];
$list = new ShoppingList('my-groceries', $items);
//Act: Add the item to the list
$actual = $list->getItems();
$list->addItem('cabbage');
//Assert: Check to see if the item was added to the list
$this->assertEquals($items, $actual);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/460515.html
上一篇:假設庫:補充一些其他策略的策略
