教程-.net中的BDD和依赖注入(4)

本文关键字:注入 依赖 net 中的 BDD 教程 | 更新日期: 2023-09-27 18:16:40

请帮我删除粗体的Mock错误。

错误1找不到类型或命名空间名称"Mock"缺少using指令或程序集参考?)C:'Users' MOB140003208 ' ' visual studio的文档2012'Projects'RockPaperScissors'RockPaperScissorsTest'Tests'GameTest.cs 65 34 RockPaperScissorsTest

错误2类型或命名空间名称"Mock"找不到(您是否缺少using指令或程序集参考?)C:'Users' MOB140003208 ' ' visual studio的文档2012'Projects'RockPaperScissors'RockPaperScissorsTest'Tests'GameTest.cs 77 34 RockPaperScissorsTest

错误3类型或命名空间名称"Mock"找不到(您是否缺少using指令或程序集参考?)C:'Users' MOB140003208 ' ' visual studio的文档2012'Projects'RockPaperScissors'RockPaperScissorsTest'Tests'GameTest.cs 89 34 RockPaperScissorsTest

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RockPaperScissors;
namespace RockPaperScissorsTest.Tests
{
/// <summary>
/// Summary description for GameTest
/// </summary>
[TestClass]
public class GameTest
{
    public GameTest()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private TestContext testContextInstance;
    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }
    #region Additional test attributes
    //
    // You can use the following additional attributes as you write your tests:
    //
    // Use ClassInitialize to run code before running the first test in the class
    // [ClassInitialize()]
    // public static void MyClassInitialize(TestContext testContext) { }
    //
    // Use ClassCleanup to run code after all tests in a class have run
    // [ClassCleanup()]
    // public static void MyClassCleanup() { }
    //
    // Use TestInitialize to run code before running each test 
    // [TestInitialize()]
    // public void MyTestInitialize() { }
    //
    // Use TestCleanup to run code after each test has run
    // [TestCleanup()]
    // public void MyTestCleanup() { }
    //
    #endregion
    [TestMethod]
    public void ReturnsCorrectMessageIfPlayerWins()
    {
        var engineMock = new **Mock**();
        engineMock.Setup(de => de.Result(Move.Rock, Move.Paper)).Returns(GameResult.PlayerWins);
        var game = new Game(engineMock.Object);
        game.PlayerMove = "Paper";
        var result = game.Result();
        engineMock.VerifyAll();
        Assert.AreEqual("Player Wins!", result);
    }
    [TestMethod]
    public void ReturnsCorrectMessageIfComputerWins()
    {
        var engineMock = new **Mock**();
        engineMock.Setup(de => de.Result(Move.Rock, Move.Scissors)).Returns(GameResult.ComputerWins);
        var game = new Game(engineMock.Object);
        game.PlayerMove = "Scissors";
        var result = game.Result();
        engineMock.VerifyAll();
        Assert.AreEqual("Computer Wins!", result);
    }
    [TestMethod]
    [ExpectedException(typeof(ArgumentNullException))]
    public void ThrowsErrorIfPlayerMoveIsNotSet()
    {
        var engineMock = new **Mock**();
        var game = new Game(engineMock.Object);
        game.Result();
    }
}

}

教程-.net中的BDD和依赖注入(4)

您是否缺少using指令或程序集引用?

是的,你是。您需要添加对包含Mock类型的程序集的引用。您还需要添加一个using语句,以便能够使用Mock类型,而不必在前面添加名称空间。

因此,请确保您有Moq程序集的引用(最好使用NuGet),并确保在源代码的顶部添加using Moq;