tdd - 单元测试用例语句
问题描述
我很感激这可能不是问的地方,如果有更好的地方请告诉我。
我有一个旧的存储过程,它选择一些数据并插入到表中。但是,我想测试很多案例陈述,我想知道应该如何做。
insert into [dbo].[utb_ITP]
...
case when [sme].[Reporting Countries] = 'Multiple Countries'
then 'United Kingdom-Multiple Countries'
when patindex('%-$',[sme].[Reporting Countries) > 1
then left([sme].[Reporting countries], (pat index('%-%', [sme].[Reporting Countries]) - 1 ))
else [sme].[Reporting Countries]
end as [SVC_COUNTRY]
我认为我看到的样本都是模拟数据和测试函数,但我还没有真正看到像案例陈述这样的例子。我要测试最终输出还是组件?所以我会先测试
[sme].[Reporting Countries] = 'Multiple Countries'
then 'United Kingdom-Multiple Countries'
我将使用 AssertEqualsTable ,其中预期值为“英国-多个国家”,然后插入实际值将运行案例的第一部分,例如:
insert into #Expected values ('United Kingdom-Multiple Countries')
insert into #Actual
select case [sme].[Reporting Countries] = 'Multiple Countries'
then 'United Kingdom-Multiple Countries'
end
from [SourceTable] as [sme]
where [sme].[Reporting Countries] = 'Multiple Countries'
exec AssertEqualsTable ...
解决方案
你在正确的轨道上。您确实希望隔离CASE
语句的这些分支。这意味着您需要为其中的每个可能分支编写一个测试。
您还希望将这些测试与其他功能隔离开来。这意味着,伪造所有涉及的表,然后插入足够的行(在这些测试中可能是 1 行)来证明这一点。tSQLt.FakeTable
允许您仅将数据放在与测试此特定功能相关的列中。在您的情况下,它看起来可能只是[sme].[Reporting Countries]
列。
所以你想写一个测试'Multiple Countries'
,两个测试PATINDEX
(例如'value-withdash'
和'othervalue-withdash'
),最后一个ELSE
分支(例如'other value without dash'
)。
在测试中,我倾向于更注意确保测试的意图是明确的,而不是值是现实的。
如果您有其他案例语句INSERT
,请按照相同的模式为这些语句编写独立的测试。
推荐阅读
- bash - bash 中文件“采购”的区别
- node.js - Is there way to run React app in nginx(Docker, nginx knowledge required)
- bash - 如何清除所有环境变量
- c# - 读取 XML 时在 LINQ 中正确使用空条件运算符
- php - 显示字符串的每隔一个符号
- python - 将 flask_injector 依赖项目部署到 Lambda 不起作用
- reactjs - 无法在 React Datepicker 上设置时间值
- rust - 如何将带有 BufReader 和 BufWriter 的 TcpStream 存储在数据结构中
- java - 如何在不使用 Java 中的 substring() 方法的情况下获取子字符串?
- php - Wordpress 自定义页面模板 Stripe 集成显示 - “PHP 致命错误:未捕获的错误:找不到类 'Stripe\Stripe'”错误”