实际上,问题是是否可以在不使用附加参数的情况下实现这一点。具体来说,在这段代码中:
public static bool CheckAndStep(
ref int x,
ref int y,
Direction direction,
ref VirtualPoint[,] map,
Point start,
bool isBack
)
{
if (
map[
x + direction.X,
y + direction.Y
].Flag < 3
|| (
isBack
&& map[
x + direction.X,
y + direction.Y
].Flag != 3
)
)
{
if (start.X != x || start.Y != y)
{
if (map[x + direction.X, y + direction.Y].Flag == 4) map[x, y].Flag = 3;
map[x + direction.X, y + direction.Y].Flag = 4;
}
if (!isBack) Logger.Write(
x
+ ", "
+ y
+ " -> "
+ (x + direction.X)
+ ", "
+ (y + direction.Y)
);
else Logger.Write(
+ (x + direction.X)
+ ", "
+ (y + direction.Y)
+ " <- "
+ x
+ ", "
+ y
);
x += direction.X;
y += direction.Y;
return true;
}
else return false;
}
我需要它只Logger.Write()在测试中工作,而不是在程序的正常执行期间工作。
不,你不应该这样。
单元测试的重点是它按原样测试您的方法。如果您正在测试一件事,而在生产中正在执行另一件事,那么为什么要进行整个测试?
如果需要调试,可以添加任何调试代码,但一定要在发现错误后将其删除。调试完成后,调试代码在您的方法中没有位置。
如果您需要记录(不仅用于调试),那么从特定记录器中抽象出来并通过这样的接口记录是有意义的
ILogger(例如,依赖注入可以负责提供特定的实现)。