模拟对象中的方法不是从 sut 调用的
Method in Mocked object is not called from sut
我为我的纸牌游戏制作 MSTest。我有 Player class(它取决于 IStrategy
接口),它有一个调用 interface.Attack()
方法的方法 Attack()
。 Mock<IStrategy>
已创建并设置 strategy.Attack()
。但是当Player.Attack
被调用时,mock.Attack
没有被调用。
我做错了什么?
public class Player : IPlayer
{
public List<Card> CardsOnHands { get; }
public IStrategy Strategy { get; }
public Player(IStrategy strategy)
{
Strategy = strategy;
CardsOnHands = new List<Card>();
}
public Card Attack(List<Card> CardsOnTable)
{
return Strategy.Attack(CardsOnHands, CardsOnTable);
}
}
我的测试如下
[TestMethod]
public void PlayerAttackShouldReturnCard()
{
//Arrange
var StrategyMock = new Mock<IStrategy>();
ExpectedCard = new Card(1, "fakeName", "fakeSuit", true);
CardList = new List<Card>
{
new Card(1, "", "", true),
new Card(1, "", "", true),
new Card(1, "", "", true)
};
StrategyMock
.Setup(x => x.Attack(CardList, CardList))
.Returns(ExpectedCard);
var _player = new Player (StrategyMock.Object);
//Act
Card actualCard = _player.Attack(CardList);
//Assert
StrategyMock.Verify(x => x.Attack(CardList, CardList), Times.Exactly(1));
Assert.AreEqual(ExpectedCard, actualCard);
}
Verify
失败
您必须显式地将模拟接口注入主题 class (_player
)。
当前示例未显示您在哪里执行此操作。
它需要看起来像
[TestMethod]
public void PlayerAttackShouldReturnCard() {
//Arrange
var strategyMock = new Mock<IStrategy>();
var expectedCard = new Card(1, "fakeName", "fakeSuit", true);
var tableCards = new List<Card> {
new Card(1, "", "", true),
new Card(1, "", "", true),
new Card(1, "", "", true)
};
strategyMock
.Setup(x => x.Attack(It.IsAny<List<Card>>(), tableCards))
.Returns(expectedCard)
.Verifiable();
Player _player = new Player(strategyMock.Object); //<--- explicit injection here
//Act
Card actualCard = _player.Attack(TableCards);
//Assert
strategyMock.Verify();
Assert.AreEqual(expectedCard, actualCard);
}
我为我的纸牌游戏制作 MSTest。我有 Player class(它取决于 IStrategy
接口),它有一个调用 interface.Attack()
方法的方法 Attack()
。 Mock<IStrategy>
已创建并设置 strategy.Attack()
。但是当Player.Attack
被调用时,mock.Attack
没有被调用。
我做错了什么?
public class Player : IPlayer
{
public List<Card> CardsOnHands { get; }
public IStrategy Strategy { get; }
public Player(IStrategy strategy)
{
Strategy = strategy;
CardsOnHands = new List<Card>();
}
public Card Attack(List<Card> CardsOnTable)
{
return Strategy.Attack(CardsOnHands, CardsOnTable);
}
}
我的测试如下
[TestMethod]
public void PlayerAttackShouldReturnCard()
{
//Arrange
var StrategyMock = new Mock<IStrategy>();
ExpectedCard = new Card(1, "fakeName", "fakeSuit", true);
CardList = new List<Card>
{
new Card(1, "", "", true),
new Card(1, "", "", true),
new Card(1, "", "", true)
};
StrategyMock
.Setup(x => x.Attack(CardList, CardList))
.Returns(ExpectedCard);
var _player = new Player (StrategyMock.Object);
//Act
Card actualCard = _player.Attack(CardList);
//Assert
StrategyMock.Verify(x => x.Attack(CardList, CardList), Times.Exactly(1));
Assert.AreEqual(ExpectedCard, actualCard);
}
Verify
失败
您必须显式地将模拟接口注入主题 class (_player
)。
当前示例未显示您在哪里执行此操作。
它需要看起来像
[TestMethod]
public void PlayerAttackShouldReturnCard() {
//Arrange
var strategyMock = new Mock<IStrategy>();
var expectedCard = new Card(1, "fakeName", "fakeSuit", true);
var tableCards = new List<Card> {
new Card(1, "", "", true),
new Card(1, "", "", true),
new Card(1, "", "", true)
};
strategyMock
.Setup(x => x.Attack(It.IsAny<List<Card>>(), tableCards))
.Returns(expectedCard)
.Verifiable();
Player _player = new Player(strategyMock.Object); //<--- explicit injection here
//Act
Card actualCard = _player.Attack(TableCards);
//Assert
strategyMock.Verify();
Assert.AreEqual(expectedCard, actualCard);
}