簡體   English   中英

在 C# 中使用 Moq 和 NUnit

[英]Using Moq with NUnit in C#

我正在構建一個簡單的機場程序,如果天氣晴朗而不是暴風雨,飛機只能起飛/降落。 這取決於 Weather 類(它在晴天和暴風雨之間隨機化天氣)。 但是,對於我的測試,我想模擬天氣,以便我可以測試所有情況。

這是我的 Weather.cs:

using System;

namespace ClassNameWeather
{
    public class Weather
    {
        public Weather()
        {

        }

        public string Forecast()
        {
            Random random = new Random();
            var weather = random.Next(1, 11);
            if (weather == 1 || weather == 2)
            {
                return "stormy";
            }
            else
            {
                return "sunny";
            }
        }
    }
}

這是我的 Airport.cs:

using System;
using System.Collections.Generic;
using ClassNamePlane;
using ClassNameWeather;

namespace ClassNameAirport
{
    public class Airport
    {
        private string _AirportName { get; set; }
        public List<Plane> planes;
        private Weather _weather = new Weather();

        public Airport(string _airportName, Weather weather)
        {
            planes = new List<Plane>();
            _AirportName = _airportName;
        }

        public void Land(Plane plane)
        {
            if (_weather.Forecast() != "stormy")
            {
                planes.Add(plane);
                Console.WriteLine($"{ plane.Name } has landed at {_AirportName}");
            }
            else
            {
                throw new Exception("It's too stormy to land");
            }
        }

        public void TakeOff(Plane plane)
        {
            if (_weather.Forecast() != "stormy")
            {
                planes.Remove(plane);
                Console.WriteLine($"{ plane.Name } has departed from {_AirportName}");
            }
            else
            {
                throw new Exception("It's too stormy to take off");
            }
        }

        public int GetPlaneCount()
        {
            Console.WriteLine($"Number of planes at {_AirportName}: {planes.Count}");
            return planes.Count;
        }

        public void GetPlaneNames()
        {
            planes.ForEach(plane => Console.WriteLine((plane as Plane).Name));
        }

        public List<Plane> GetPlaneList()
        {
            return planes;
        }
    }
}

這是我嘗試在其中使用模擬的測試:

using NUnit.Framework;
using ClassNameAirport;
using ClassNamePlane;
using ClassNameWeather;
using Moq;

namespace AirportTest
{
    public class AirportTest
    {
        Airport airport = new Airport("TestAirport", weather);
        Plane plane = new Plane("TestPlane");

        [Test]
        public void PlaneCanLand()
        {
            var weather = new Mock<Weather>();
            weather.Setup(x => x.Forecast()).Returns("sunny");
            airport.Land(plane);
            Assert.IsTrue(airport.planes.Contains(plane));
        }

        public void PlaneCanTakeOff()
        {
            airport.Land(plane);
            airport.TakeOff(plane);
            Assert.IsFalse(airport.planes.Contains(plane));
        }
    }
}

這一行: Airport airport = new Airport("TestAirport", weather); 不工作,說天氣這個名字不存在。

任何人都可以幫助我確保我正確使用 Moq 嗎? 我是 C# 的新手,非常感謝任何建議。

謝謝!

更新我已修復此問題,但現在收到以下錯誤:

System.NotSupportedException : Unsupported expression: x => x.Forecast()
Non-overridable members (here: Weather.Forecast) may not be used in setup / verification expressions.

有誰知道如何解決這個問題?

你可以引入接口IWeather類的

public interface IWeather
{
     string Forecast();
}

比在 Weather 類中實現它。 IWeather引用傳遞給AirPort類並為此設置模擬。

var weather = new Mock<IWeather>();
weather.Setup(x => x.Forecast()).Returns("sunny");
...
var airport = new Airport("TestAirport", weather.Object)

並且不要直接在Airport類中初始化它private Weather _weather = new Weather(); (不使用您的構造函數參數),這樣做

public class Airport
{
    private string _AirportName { get; set; }
    public List<Plane> planes;
    private readonly IWeather _weather; 

    public Airport(string _airportName, IWeather weather)
    {
        planes = new List<Plane>();
        _weather = weather;
    }
...
}

您尚未聲明變量weather 我建議您創建一個 Initialize 方法並將其屬性為TestInitialze

[TestInitialize]
public void TestInitialize() 
{
    var weather = new Mock<Weather>();
    var airport = new Airport("TestAirport", weather)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM