单元测试if else条件 [英] Unit test if else condition

查看:1078
本文介绍了单元测试if else条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码集



I have got the below set of code

if (entity.InterpreterCostAuthorised && entity.InterpreterCostAuthorisedDate == null)
          {
              entity.InterpreterCostAuthorisedDate = DateTime.UtcNow;
          }
          else if (!entity.InterpreterCostAuthorised)
          {
              entity.InterpreterCostAuthorisedDate = null;
          }

          if (entity.ClientChargeAuthorised && entity.ClientChargeAuthorisedDate == null)
          {
              entity.ClientChargeAuthorisedDate = DateTime.UtcNow;
          }
          else if (!entity.ClientChargeAuthorised)
          {
              entity.ClientChargeAuthorisedDate = null;
          }





可以帮助我为这些陈述编写单元测试



我尝试了什么:



i尝试使用不同的测试用例和另外两个list



Can any on help me to write unit test for these set of statements

What I have tried:

i tried by using different test case and also two more list

推荐答案

一次选择一个场景,设置实体属性以满足该场景,然后检查与场景匹配的实体属性是否已设置。



例如,您首先要检查InterpreterCostAuthorised是否为真并且InterpreterCostAuthorisedDate为空,因此在实体上设置这些属性,然后检查InterpreterCostAuthorisedDate是否有日期,并且可能检查日期是否在当前日期的1分钟之内。这是所有直截了当的东西,所以我不确定究竟是什么问题?
Pick one scenario at a time, set the entity properties up such that that scenario is satisfied, then check the entity properties that match the scenario have been set up.

eg you'll first want to check when InterpreterCostAuthorised is true and InterpreterCostAuthorisedDate is null, so set those properties on the entity, then check that InterpreterCostAuthorisedDate has a date, and maybe check the date is within 1 minute of the current date. This is all straight forward stuff so I'm not sure exactly what the issue is?


将有两种单元测试方法



1.首先检查InterpreterCostAuthorisedDate是否设置了当前时间/你可以检查日期字段是否为空
There will be two unit test methods

1. first will check whether InterpreterCostAuthorisedDate is set with current time or not / you can check whether date field is null or not
Assert.IsNotNull(InterpreterCostAuthorisedDate)





2.秒将检查InterpreterCostAuthorisedDate是否设置为null

你可以使用



2. second will check whether InterpreterCostAuthorisedDate is set null or not
you can assert it using

Assert.Isnull()





希望这会对你有帮助。



Hope this will help you.


这篇关于单元测试if else条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆