php 中的 addItem 单元测试
addItem unit test in php
我正在尝试测试是否可以将商品添加到我的购物清单中。我不确定我哪里出错了,我得到的错误是:
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,其中包含我正在测试的功能:
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;
}
这是通过的测试。我没有向列表中添加其他项目。
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);
}
我正在尝试测试是否可以将商品添加到我的购物清单中。我不确定我哪里出错了,我得到的错误是:
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,其中包含我正在测试的功能:
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;
}
这是通过的测试。我没有向列表中添加其他项目。
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);
}