【问题标题】:How to write unit test case for BadRequest?如何为 BadRequest 编写单元测试用例?
【发布时间】:2016-08-24 06:26:36
【问题描述】:

我想为以下代码编写单元测试用例

HomeController.cs

[HttpPost]
        [ActionName("CreateDemo")]
        public async Task<IHttpActionResult> CreateDemo([FromBody] MyRequest request)
        {
            if (request == null)
            {                    
                return BadRequest("request can not be null");
            }
            if (request.MyID == Guid.Empty)
            {
                return BadRequest("MyID must be provided");
            }
        }

我尝试了以下方法,我猜这是不正确的

 [TestMethod]
        public async Task NullCheck()
        {
            try
            {
                var controller = new HomeController();
                var resposne = await controller.CreateDemo(null);
                Assert.AreEqual(); // not sure what to put here
            }
            catch (HttpResponseException ex) //catch is not hit
            {
                Assert.IsTrue(
                     ex.Message.Contains("request can not be null"));
            }

        }

【问题讨论】:

    标签: c# unit-testing nunit testcase


    【解决方案1】:

    每个单元测试都应测试一个需求或关注点。您的方法实现了两个要求:

    1) 如果请求是null,则返回带有预定义错误消息的BadRequestErrorMessageResult 对象。 2) 如果请求的MyID 属性为空GUID,则返回BadRequestErrorMessageResult 对象以及另一个预定义的错误消息。

    这意味着我们应该有两个单元测试:

    [Test]
    public async Task CreateDemo_returns_BadRequestErrorMessageResult_when_request_is_null()
    {
       // Arrange
       var controller = new HomeController();
    
       // Act
       var response = await controller.CreateDemo(null);
    
       // Assert
       Assert.IsInstanceOf<BadRequestErrorMessageResult>(response);
       Assert.AreEqual("request can not be null", response.Message);
    }
    
    [Test]
    public async Task CreateDemo_returns_BadRequestErrorMessageResult_when_request_ID_is_empty_GUID()
    {
       // Arrange
       var controller = new HomeController();
       var request = new MyRequest(Guid.Empty);
    
       // Act
       var response = await controller.CreateDemo(request);
    
       // Assert
       Assert.IsInstanceOf<BadRequestErrorMessageResult>(response);
       Assert.AreEqual("MyID must be provided", response.Message);
    }
    

    您可以更进一步,将这些测试中的每一个拆分为两个,一个测试返回对象是否为预期类型,另一个验证返回对象状态是否符合预期(例如,Message 字符串是否符合预期)。这样,每个测试都会有一个断言。

    旁注:

    您用nunit 标记了这个问题,所以我提供了使用该框架的代码。不过,在您的示例中,您使用来自 Microsoft 单元测试框架的 [TestMethod] 属性。如果您想使用该框架,则必须进行一些更改,例如将Assert.IsInstanceOf 替换为Assert.IsInstanceOfType

    我假设GUID 通过其构造函数传递给MyRequest,该构造函数将其分配给MyID

    我不是来自网络世界,但我发现 BadRequest 方法有一个重载,如果 string 作为其参数传递,则返回 BadRequestErrorMessageResult

    【讨论】:

    • 谢谢,如何使用IsInstanceOfType比较消息?
    • 对于字符串断言,使用Assert.AreEqual,或者作为Nunit3 advises,使用具有相等约束的约束模型:Assert.That(response.Message, Is.EqualTo("MyID must be provided"));
    • 我认为它对 XUnit 也同样有效?但在我看来,Assert在 XUnit 框架中没有IsInstanceOf...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 2020-09-19
    • 2020-06-30
    相关资源
    最近更新 更多