Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未从sut调用模拟对象中的方法_C#_Unit Testing_Moq - Fatal编程技术网

C# 未从sut调用模拟对象中的方法

C# 未从sut调用模拟对象中的方法,c#,unit-testing,moq,C#,Unit Testing,Moq,我为我的纸牌游戏做MST测试。我有一个Player类(取决于IStrategyinterface),它有一个调用interface.Attack()方法的方法Attack()模拟已创建并设置策略.攻击()。但是当调用Player.Attack时,mock.Attack不被调用 我做错了什么 public class Player : IPlayer { public List<Card> CardsOnHands { get; } pu

我为我的纸牌游戏做MST测试。我有一个Player类(取决于
IStrategy
interface),它有一个调用
interface.Attack()
方法的方法
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);
        }
    }
公共类玩家:IPlayer
{
公共列表CardsOnHands{get;}
公共战略战略{get;}
公众参与者(IST战略)
{
战略=战略;
CardsOnHands=新列表();
}
公共卡攻击(列表卡列表)
{
返回策略。攻击(卡片手,卡片桌);
}
}
我的测试如下

    [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);
    }
[TestMethod]
公共作废PlayerTackShouldReturnCard()
{
//安排
var StrategyMock=new Mock();
ExpectedCard=新卡(1,“fakeName”、“fakeSuit”,真);
CardList=新列表
{
新卡(1,“,”,正确),
新卡(1,“,”,正确),
新卡(1、、、真)
};
兵工厂
.Setup(x=>x.Attack(CardList,CardList))
.返回(预期卡);
var _player=新玩家(StrategyMock.Object);
//表演
卡片实际卡片=_玩家攻击(卡片列表);
//断言
StrategyLock.Verify(x=>x.Attack(CardList,CardList),Times.justice(1));
断言.AreEqual(预期卡片、实际卡片);
}

验证
失败

您必须显式地将模拟接口注入主题类(
\u 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);
}
[TestMethod]
公共作废PlayerTackShouldReturnCard(){
//安排
var strategyMock=new Mock();
var expectedCard=新卡(1,“fakeName”、“fakeSuit”,真);
var tableCards=新列表{
新卡(1,“,”,正确),
新卡(1,“,”,正确),
新卡(1、、、真)
};
兵工厂
.Setup(x=>x.Attack(It.IsAny(),tableCards))
.退货(预期卡)
.可验证();

Player _Player=new Player(strategyMock.Object);//您必须将模拟接口显式地注入主题类(
_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);
}
[TestMethod]
公共作废PlayerTackShouldReturnCard(){
//安排
var strategyMock=new Mock();
var expectedCard=新卡(1,“fakeName”、“fakeSuit”,真);
var tableCards=新列表{
新卡(1,“,”,正确),
新卡(1,“,”,正确),
新卡(1、、、真)
};
兵工厂
.Setup(x=>x.Attack(It.IsAny(),tableCards))
.退货(预期卡)
.可验证();

Player _Player=new Player(strategyMock.Object);//真正的问题是我使用了相同的列表(x.Attack(CardList,CardList))而不是它。IsAny()。替换修复了代码。真正的问题是我使用了相同的列表(x.Attack(CardList,CardList)。IsAny()替换修复了代码。