单元测试未涵盖的 LINQ`Any`
LINQ `Any` not covered by unit test
我有这个方法:
/// <summary>
/// Moves piece to location, does not verify that this is a valid move
/// </summary>
/// <param name="dest"></param>
public virtual void Move(Coordinate dest)
{
if (Board.IsPieceAtLocation(dest))
{
if (GetAllPossibleKills().Any(p => p.BoardLocation == dest))
{
Board.KillPieceAtLocation(dest);
}
else
{
throw new LocationOccupiedException("Board location already occupied and kill is not valid",this, Board.GetPieceAtLocation(dest));
}
}
BoardLocation = dest;
}
它包含在两个单元测试中:
[Test]
public void LocationOccupiedTest()
{
Board board = new Board();
board.GenerateNewGamePieces();
Assert.Throws<LocationOccupiedException>(()=>board.GetKing(false).Move(new Coordinate(0,0)));
}
[Test]
public void KillPieceTest()
{
Board board = new Board();
board.GenerateNewGamePieces();
Piece knight = board.GetPieceAtLocation(new Coordinate(1, 0));
knight.Move(new Coordinate(1,4));
Assert.DoesNotThrow(()=>knight.Move(new Coordinate(0,6)));
}
根据覆盖率分析,整个方法都被覆盖了,除了:
if (GetAllPossibleKills().Any(p => p.BoardLocation == dest))
您可以在每行代码旁边看到覆盖状态:
鉴于遍历了 if 语句的两个分支,我不明白怎么会这样。
我怎样才能得到这个?
我怀疑问题是您没有测试 Any
.
所有可能的执行路径
您可能需要添加额外的测试用例以确保您涵盖(例如)存在其他有效目标时的非法移动(感谢示例@Persistence)。
要修复此特定示例,您需要添加一个测试用例,以便在存在其他有效杀戮目标时移动到不是有效杀戮目标的占用 space。
[Test]
public void MoveToOccupiedWithOtherValidTargets()
{
Board board = new Board();
board.GenerateNewGamePieces();
Piece king = board.GetKing(true);
king.Move(new Coordinate(0,5));
board.GetPieceAtLocation(new Coordinate(0,1)).Move(new Coordinate(0,4));
Assert.Throws<LocationOccupiedException>(()=>king.Move(new Coordinate(0,4)));
}
我有这个方法:
/// <summary>
/// Moves piece to location, does not verify that this is a valid move
/// </summary>
/// <param name="dest"></param>
public virtual void Move(Coordinate dest)
{
if (Board.IsPieceAtLocation(dest))
{
if (GetAllPossibleKills().Any(p => p.BoardLocation == dest))
{
Board.KillPieceAtLocation(dest);
}
else
{
throw new LocationOccupiedException("Board location already occupied and kill is not valid",this, Board.GetPieceAtLocation(dest));
}
}
BoardLocation = dest;
}
它包含在两个单元测试中:
[Test]
public void LocationOccupiedTest()
{
Board board = new Board();
board.GenerateNewGamePieces();
Assert.Throws<LocationOccupiedException>(()=>board.GetKing(false).Move(new Coordinate(0,0)));
}
[Test]
public void KillPieceTest()
{
Board board = new Board();
board.GenerateNewGamePieces();
Piece knight = board.GetPieceAtLocation(new Coordinate(1, 0));
knight.Move(new Coordinate(1,4));
Assert.DoesNotThrow(()=>knight.Move(new Coordinate(0,6)));
}
根据覆盖率分析,整个方法都被覆盖了,除了:
if (GetAllPossibleKills().Any(p => p.BoardLocation == dest))
您可以在每行代码旁边看到覆盖状态:
鉴于遍历了 if 语句的两个分支,我不明白怎么会这样。
我怎样才能得到这个?
我怀疑问题是您没有测试 Any
.
您可能需要添加额外的测试用例以确保您涵盖(例如)存在其他有效目标时的非法移动(感谢示例@Persistence)。
要修复此特定示例,您需要添加一个测试用例,以便在存在其他有效杀戮目标时移动到不是有效杀戮目标的占用 space。
[Test]
public void MoveToOccupiedWithOtherValidTargets()
{
Board board = new Board();
board.GenerateNewGamePieces();
Piece king = board.GetKing(true);
king.Move(new Coordinate(0,5));
board.GetPieceAtLocation(new Coordinate(0,1)).Move(new Coordinate(0,4));
Assert.Throws<LocationOccupiedException>(()=>king.Move(new Coordinate(0,4)));
}