安排法案断言 [英] Arrange Act Assert Alternatives

查看:89
本文介绍了安排法案断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般的问题是是否有用于AAA的单元测试替代模式? 如果是的话,看一些示例并了解其优缺点将非常有趣.

The general question is are there alternative patterns to AAA for unit testing? If, yes, would be very interesting to see some examples and hear about their pros and cons.

作为AAA测试的最简单示例(在c#中,为简单起见,使用var):

And as the simplest example of AAA test (in c#, using var for the sake of simplicity):

// Arranging
var annualSalary = 120000M;
var period = 3; // for a quarter profit
var calc = new SalaryCalculator();

// Acting
var result = calc.CalculateProfit(annualSalary, period); 

// Assertion
Assert.IsEqual(40000, result);

推荐答案

我喜欢的东西并不是AAA的替代品,而是一种变体.我认为它是 Arrange-Assert(不是)-Act-Assert ,但其他人将其称为 Guard Assertion .这个想法是要有一个断言,以确保在该法案之前尚不存在该法案的预期结果. 此处,对此进行了很好的讨论.

I like something that is not so much an alternative to AAA, but rather a variation. I think of it as Arrange-Assert(not)-Act-Assert, but others have called it a Guard Assertion. The idea is to have an assertion that ensures the desired result of the Act is not already present before the act. There's a good discussion of it here.

这篇关于安排法案断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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