我总是试图坚持每个测试的一个断言,但有时候我会遇到麻烦。输出是另一个输入的单元测试方法
例如。
假设我编写了一个加密和解密字符串的加密类。
public class CryptoDummy
{
public string Decrypt(string value)
{
}
public string Encrypt(string value)
{
}
}
我将如何创建我的单元测试,如果解密后的加密输出依赖?
我的大部分测试(如果不是全部到目前为止)都是由每个测试的一个方法调用和每个测试的一个断言组成的。
因此,对于每次测试多次调用并声明最后一次调用的方法所产生的最终结果是否正确?
public class CryptoDummyTest
{
private static CryptoDummy _cryptoDummy;
// Use ClassInitialize to run code before running the first test in the class
[ClassInitialize]
public static void MyClassInitialize(TestContext testContext)
{
_cryptoDummy = new CryptoDummy();
}
[TestMethod]
public void Encrypt_should_return_ciphered_64string_when_passing_a_plaintext_value()
{
const string PLAINTEXT_VALUE = "[email protected]";
string cipheredString = _cryptoDummy.Encrypt(PLAINTEXT_VALUE);
Assert.IsTrue(cipheredString != PLAINTEXT_VALUE);
}
[TestMethod]
public void Decrypt_should_return_plaintext_when_passing_a_ciphered_value()
{
const string PLAINTEXT_VALUE = "[email protected]";
string cipheredString = _cryptoDummy.Encrypt(PLAINTEXT_VALUE);
string plaintextString = _cryptoDummy.Decrypt(cipheredString);
Assert.IsTrue(plaintextString == PLAINTEXT_VALUE);
}
}
在此先感谢您。
有一篇由Marc Clifton撰写的文章(我最喜欢的一篇)关于单元测试模式[Here](http://www.codeproject.com/KB/architecture/autp5.aspx) –
谢谢,我已阅读并且很久以前购买了这本书的单元测试艺术。 ;) – LavaSeven