首页 > 解决方案 > 在 Nunit 中测试异常

问题描述

我有以下我正在尝试测试的异常

     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");
            }
        }

但是我似乎找不到正确的语法来测试这个异常?通过四处搜索,我到目前为止有:

Assert.Throws(Is.TypeOf<Exception>())
                 .And.Message.EqualTo("Too stormy to land");

但是它说不能从用法中推断出 Throws。

任何人都可以帮忙吗?(抱歉,对 C# 很陌生!)谢谢 :)

标签: c#nunit

解决方案


推荐阅读