假设我有包含内容的 ac 源文件(正在测试的源文件):
uint8 reg1 = I2CRead(20)
uint8 reg2 = I2CRead(24)
uint8 reg3 = I2CRead(28)
if (reg1 != 0x10) || (reg2 != 0x11) || (reg3 != 0x12)
{
register content wrong ...
}
else
{
register content is OK...
}
I2CRead() 函数构造如下:
uint8 I2CRead (uint8 address)
{
I2C_Hw_Istance()->DTR = address //DTR = Data Transmit Register
return I2C_Hw_Instance()-> DRR //DRR = Data Receive Register
}
现在我正在尝试编写一个单元测试(使用 cpputest 框架),我想“伪造”函数 I2CRead()读回的值,以便满足 if() 中的所有相等表达式。
单元测试文件与“被测源文件”隔离,但我可以通过特殊方式访问被测源文件中的以下函数:I2C_Hw_Instance()。
使用此功能,我可以通过以下方式“伪造”单元测试文件中的寄存器值:
I2C_Hw_Istance()->DTR = 20;
I2C_Hw_Istance()->DRR = 0x10;
因为我连续调用了 3 个I2CRead()函数,所以我需要通过每个I2CRead()单独调用来伪造 DTR 和 DRR 。因此,这意味着单元测试文件需要知道何时调用下一个I2CRead()函数才能操作 DTR 和 DRR 中的值。
一般如何做到这一点,有什么想法吗?
我的想法是拥有一种与单元测试一起构建的假文件(只是“概念”):
假文件.c
void fakeRegisters()
{
if first call of I2CRead() than:
I2C_Hw_Istance()->DTR = 20;
I2C_Hw_Istance()->DRR = 0x10;
if second call of I2CRead()than:
I2C_Hw_Istance()->DTR = 24;
I2C_Hw_Istance()->DRR = 0x11;
if tird call of I2CRead() than:
I2C_Hw_Istance()->DTR = 28;
I2C_Hw_Istance()->DRR = 0x12;
}