php單元測試進階(13)- 核心技術(shù) - mock對象 - 同時使用mock和stub
本系列文章主要代碼與文字來源于《單元測試的藝術(shù)》,原作者:Roy Osherove。譯者:金迎。
本系列文章根據(jù)php的語法與使用習(xí)慣做了改編。所有代碼在本機測試通過。如轉(zhuǎn)載請注明出處。
假設(shè)需求變更,更加復(fù)雜一些。
如文件名過短,則web服務(wù)記錄日志,但萬一記錄過程中發(fā)生異常,需發(fā)送一封郵件。
要求測試發(fā)送郵件是成功的。
源代碼有2個接口,一個被測類。
測試代碼有2個偽對象類,一個測試類。
源代碼
(1)\t2\application\index\controller下,錯誤日志接口
IWebService.php
<?php
namespace app\index\controller;
/**
* 記錄錯誤日志的接口,供mock對象和真正的對象實現(xiàn)
*/
interface IWebService
{
/**
* 記錄錯誤日志
* @param string $message
*/
public function logError($message);
}
(2)\t2\application\index\controller下,郵件接口
IEmailService.php
<?php
namespace app\index\controller;
/**
* 郵件的接口,供mock對象和真正的對象實現(xiàn)
*/
interface IEmailService
{
/**
* 發(fā)送郵件
*
* @param string $to
* @param string $subject
* @param string $body
*/
public function sendEMail ($to, $subject, $body);
}
(3)被測類,實現(xiàn)萬一拋異常,就發(fā)郵件這個功能。\t2\application\index\controller下,
LogAnalyzer.php
<?php
namespace app\index\controller;
/**
* 日志分析器類,也是被測類
*
* 這是同時使用mock對象和樁件的例子。
*/
class LogAnalyzer
{
/**
* @var IWebService
*/
private $service;
/**
* @var IEmailService
*/
private $email;
/**
* 構(gòu)造方法注入服務(wù)
* @param IWebService $service
* @param IEmailService $email
*/
public function __construct(IWebService $service, IEmailService $email)
{
$this->service = $service;
$this->email = $email;
}
/**
* 分析日志,省略無關(guān)功能,檢查文件名過短,記錄錯誤日志,可能發(fā)生異常。
* @param string $filename
*/
public function analyze($filename)
{
if (strlen($filename) < 8 ) {
try {
$this->service->logError("Filename too short:{$filename}");
} catch ( \Exception $e ) {
$this->email->sendEMail("someone@somewhere.com", "can not log", $e->getMessage());
}
}
// 做一些其他的事情。
// ... ...
}
}
測試代碼
(4)\t2\tests\index\controller下,實現(xiàn)錯誤日志接口的樁件類
FakeWebService.php
<?php
namespace tests\index\controller;
/**
* 樁件類,要能拋異常,為了測試用
*/
class FakeWebService implements \app\index\controller\IWebService
{
/**
* @var \Exception
*/
public $toThrow;
/**
* 記錄錯誤日志,但是沒有偽實現(xiàn),只是可能拋異常
* @param string $message
*/
public function logError($message)
{
// 字段由外部注入,注入就拋異常
if ($this->toThrow) {
throw $this->toThrow;
}
}
}
(5)\t2\tests\index\controller下,實現(xiàn)郵件接口的mock類,要斷言的
FakeEmailService.php
<?php
namespace tests\index\controller;
/**
* mock類,要能判斷狀態(tài)。
*/
class FakeEmailService implements \app\index\controller\IEmailService
{
/**
* @var string
*/
public $to;
/**
* @var string
*/
public $subject;
/**
* @var string
*/
public $body;
/**
* 發(fā)送郵件,偽實現(xiàn)
*
* @param string $to
* @param string $subject
* @param string $body
*/
public function sendEMail ($to, $subject, $body)
{
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
}
}
(6)測試類,主要斷言了拋異常時,郵件發(fā)送成功。\t2\tests\index\controller下,
LogAnalyzerTest.php
<?php
namespace tests\index\controller;
/**
* 測試用的類
*/
class LogAnalyzerTest extends \think\testing\TestCase
{
/**
* @test
* 使用樁件模擬web服務(wù),并在其拋異常后 對mock對象斷言
* 注意,盡量使得測試的方法名稱有意義,這非常重要,便于維護測試代碼。有規(guī)律
*/
public function analyze_WebServiceThrows_SendEmail()
{
//創(chuàng)建樁件,并配置使其能拋異常
$stubService = new FakeWebService();
$stubService->toThrow = new \Exception("fake exception");
//創(chuàng)建mock對象,好斷言
$mockEmail = new FakeEmailService();
// 創(chuàng)建被測類的對象,注入mock對象和樁件
$analyzer = new \app\index\controller\LogAnalyzer($stubService, $mockEmail);
$tooShortFileName= 'abc.ext';
//調(diào)用被測對象
$analyzer->analyze($tooShortFileName);
// 注意是對mock對象斷言??!
$this->assertEquals($mockEmail->to, "someone@somewhere.com");
$this->assertEquals($mockEmail->subject, "can not log");
$this->assertEquals($mockEmail->body, "fake exception");
}
}
cmd下測試通過。
總結(jié)
原作者認為:
- 一個測試中,應(yīng)該最多只有一個mock對象,所有其他偽對象都應(yīng)該是樁件。如有多個mock對象,應(yīng)分成多個測試,確保每個測試只有一個mock對象。
- 一個測試只能斷言工作單元三種最終結(jié)果中的一種。3種結(jié)果是,斷言返回值,斷言對象或系統(tǒng)狀態(tài),斷言對象交互。目的要明確。如果有多個不同的測試意圖,應(yīng)分成多個測試。