简体   繁体   English

有关单元测试的基本主张

[英]basic assertions about unit testing

Given the function below - would I be able to assert if the Value "value" was deleted from the user? 给定下面的功能-我是否可以断言“值”是否已从用户中删除? Or is that assertion part of the tests on the UserService? 还是该断言是UserService上测试的一部分?

Also, what assertions could I test from the function? 另外,我可以从该函数测试哪些断言?

    public IHttpActionResult Post(string value)
    {
        var user = authorizationService.GetCurrentUser();

        var isDeleted = userService.DeleteValue(value, user);
        if (!isDeleted)
        {
            return NotFound();
        }

        userService.DeleteProperty(value, user);

        var identityResult = userService.Update(user);
        if (identityResult.Succeeded)
        {
            return Ok();
        }

        return InternalServerError();
    }

Yes, you can test that: a unit test doesn't have to be the smallest possible unit per sé but that it may also contain another unit inside of it. 是的,您可以测试:单元测试不必是每sé可能的最小单元,但是它也可以在其中包含另一个单元。 By writing tests for both these scenarios you're essentially making a layered project where you have separate tests for the different layers and the value they add to your chain. 通过为这两种情况编写测试,您实际上就是在创建一个分层项目,其中您对不同的层及其为链增加的价值进行单独的测试。

You could make the analogy with VAT in a production chain where each test will test the value added in each segment. 您可以在生产链中用增值税进行类比,在该生产链中,每个测试将测试每个细分市场中增加的价值。 More information on that here . 有关此的更多信息。

This translates to your question as: yes, you can (and should) test whether this action method does what it is supposed to do. 这转化为您的问题:是的,您可以(并且应该)测试此操作方法是否完成了应做的事情。

A few examples of tests you could do: 您可以执行的一些测试示例:

  • value is null value null
  • value is not in the expect format (numeric, negative value, special characters) value不是期望的格式(数字,负值,特殊字符)
  • The user is not found 找不到用户
  • a valid value will display Ok() 有效的value将显示Ok()

What you will have to do is make sure that you are not testing against a production database but instead use in-memory repositories (mocking, faking, stubbing, seaming, you name it). 您需要做的是确保您不针对生产数据库进行测试,而是使用内存中的存储库(嘲笑,伪造,存根,缝合)。

By having these tests in your controller and inside your userService you will know the exact location of the problem in case it isn't working as you want it to: 通过在控制器中以及在userService进行这些测试,您可以知道问题的确切位置,以防其无法正常运行:

Test for:   Controller           Service              Conclusion
            Works                Works                Works
            Works                Doesn't work         Faulty tests
            Doesn't work         Works                Controller test failed
            Doesn't work         Doesn't work         Service test failed

While not writing a test for the controller but instead relying on the service would not give you the information that your code actually works. 虽然没有为控制器编写测试,而是依靠服务,却无法为您提供代码实际起作用的信息。

The line between unit-testing and other types (integration and acceptance testing mainly) is thin because you're testing a bigger part of the system but I still consider it unit-testing since it is contained to the logic of your application and does not use any external resources (database calls get mocked/stubbed/...). 单元测试和其他类型(主要是集成和验收测试)之间的界限很窄,因为您正在测试系统的较大部分,但是我仍然认为它是单元测试,因为它包含在应用程序的逻辑中,并且不使用任何外部资源(数据库调用被嘲笑/存根/ ...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM