我正在尝试使用 PhpUnit/Mockery/Laravel 进行单元测试。 这并不容易。我已经阅读了数十篇教程,但仍然无法将其应用到现实生活中。
我将展示一段我想测试的代码。谁能告诉我如何测试 SoldProductModifier 类的方法 modifyBasedOnItemCode()?
先简单解释一下: 我希望用户能够输入产品代码(项目代码)和数量,我希望系统自动更新 SoldProduct 模型的 product_id 和 category_id 属性。为此,我创建了我现在要测试的类。
另请参阅: simplified diagram for my database (only tables related to my question)
现在相关代码:
要测试的类
use App\Models\Product; class SoldProductModifier { private $sold_product; public function __construct(SoldProduct $sold_product) { $this->sold_product = $sold_product; } public function modifyBasedOnItemCode($item_code) { if (! isset($item_code) || $item_code == '') { $product = Product::findByItemCode($item_code); if (isset($product) && $product != false) { $this->sold_product->category_id = $product->category->id; $this->sold_product->product_id = $product->id; } } return $this->sold_product; } }
产品型号
... public static function findByItemCode($item_code) { return self::where('item_code', $item_code)->first(); } ...
我的 Controller 引用 SUT
... $sold_product = new SoldProduct($request->all()); $modifier = new SoldProductModifier($sold_product); $sold_product = $modifier->modifyBasedOnItemCode($request->item_code); $sold_product->save(); ...
我的测试课
class SoldProductModifierTest extends TestCase { public function setUp() { parent::setUp(); $this->soldProductMock = $this->mock('App\Models\SoldProduct'); $this->productMock = $this->mock('App\Models\Product'); } public function tearDown() { Mockery::close(); } public function testDoesNotModifyIfItemCodeEmpty() { $soldProductModifier = new SoldProductModifier($this->soldProductMock); $modifiedSoldProduct = $soldProductModifier->modifyBasedOnItemCode(''); $this->assertEquals($this->soldProductMock, $modifiedSoldProduct); } public function testModifiesBasedOnItemCode() { // how do I test positive case scenario ? ...
我粘贴了我的第一个测试,以防有人认为这不是应该的方式,并且很乐意建议另一种方法来解决这个问题。
但现在我的问题是:
如何在此处模拟对数据库的调用:Product::findByItemCode($item_code)?
我是否应该在我的 SoldProductModifier 中创建一个 $product 属性并使用为此目的创建的 setter 方法设置它,例如:
public function setProduct(Product $product) { $this->product = $product; }
然后在我的 Controller 中添加额外的行:
... $modifier = new SoldProductModifier($sold_product); $modifier->setProduct(Product::findByItemCode($item_code)); // --- extra line $sold_product = $modifier->modifyBasedOnItemCode(); // --- parameter removed ...
?
我试图让我的 Controller 尽可能纤薄,所以想避免这种情况? 那么解决这种情况的最佳方法是什么?
谢谢
最佳答案
你应该通过构造函数注入(inject) Product
以便 Laravel 可以为你处理。
use App\Models\Product;
class SoldProductModifier
{
private $sold_product;
protected $product;
public function __construct(SoldProduct $sold_product, Product $product)
{
$this->sold_product = $sold_product;
$this->product = $product;
}
}
现在您需要为函数的每个“路径”编写一个单元测试。
// Build your mock object.
$mockProduct = Mockery::mock(new App\Models\Product);
// Have Laravel return the mocked object instead of the actual model.
$this->app->instance('App\Models\Product', $mockProduct);
// Tell your mocked instance what methods it should receive.
$mockProduct
->shouldReceive('findByItemCode')
->once()
->andReturn(false);
// Now you can instantiate your class and call the methods on it to be sure it's returning items and setting class properties correctly.
您应该多次编写此测试并让您的 $mockProduct
返回不同的内容,直到覆盖所有代码行。例如,您可能想要执行以下操作...
$product = new stdClass;
$product->id = 45;
$category = new stdClass;
$category-id = 60;
$product->category = $category;
$mockProduct
->shouldReceive('findByItemCode')
->once()
->andReturn($product);
函数运行后,您需要确保 sold_product->category_id
等于 60 并且 sold_product->product_id
等于 45。如果它们是私有(private)的,您无法从测试中检查它们,您可能想为这些对象编写一个 getter,以便您可以更轻松地从测试中查看它们的值。
编辑
关于您的评论,您将使用以下内容。
new SoldProductModifier($sold_product, new Product);
然后你的函数应该看起来像...
public function modifyBasedOnItemCode($item_code)
{
if (! isset($item_code) || $item_code == '')
{
$product = $this->product->findByItemCode($item_code);
if (isset($product) && $product != false)
{
$this->sold_product->category_id = $product->category->id;
$this->sold_product->product_id = $product->id;
}
}
return $this->sold_product;
}
我看到它是一个静态函数,因此您可能希望以不同的方式处理它。如果只是因为这个原因它是静态的,那么你就不能让它成为静态的。如果其他事情取决于它,您可能会创建一个非静态的新函数,它通过 self::findByItemCode($id)
这里的一般经验法则是,除非它是在你的 config.php
文件中设置的外观,否则你应该让 Laravel 为你处理注入(inject)它。这样当你测试时,你可以创建模拟对象,然后通过 $this->app->instance()
让 Laravel 知道它们,这样它就会注入(inject)那些代替真实的对象。
关于php - 模拟数据库查询 laravel mock ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36575465/