我有这个方法:
/// <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 语句的两个分支都被遍历,我不明白这是怎么回事。
我怎样才能得到这个覆盖?