我对解压缩方法的测试有问题。
我的单元测试方法类似于:
[Fact]
public void UnzipData_CalledWithByteArrayParameter_ReturnsString()
{
_serializationService.CallerName = "";
byte[] array = new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 0, 11, 117, 205, 49, 15, 130, 48, 16, 134, 255, 255, 114, 43, 148, 220, 157, 165, 45, 55, 234, 204, 98, 216, 140, 3, 9, 23, 37, 209, 18, 67, 53, 38, 134, 255, 46, 168, 139, 3, 243, 247, 189, 121, 14, 47, 232, 159, 32, 148, 195, 110, 136, 73, 99, 170, 135, 78, 47, 32, 47, 136, 237, 85, 65, 160, 214, 211, 185, 141, 144, 195, 163, 31, 251, 52, 126, 174, 93, 155, 150, 137, 145, 130, 65, 103, 152, 26, 44, 165, 172, 4, 109, 65, 155, 210, 57, 79, 25, 178, 32, 206, 213, 114, 221, 235, 237, 174, 99, 210, 238, 175, 129, 105, 202, 191, 56, 175, 226, 219, 97, 13, 174, 12, 178, 97, 219, 80, 16, 14, 98, 185, 32, 31, 188, 117, 62, 67, 90, 133, 127, 205, 12, 31, 223, 154, 207, 196, 62, 247, 0, 0, 0 };
string result = _serializationService.UnzipData(array);
Assert.False(result.Length == 0);
Assert.True(result.Length > array.Length);
}结果变量为null。Sut based on this answer
public string UnzipData(byte[] bytes)
{
LoadDictionaries();
CallerName = _messageService.GetCallerName();
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
try
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
CopyTo(gs, mso);
}
return Encoding.UTF8.GetString(mso.ToArray());
}
catch (Exception e)
{
_logger.Error(e, MessagesError[CallerName]);
return null;
}
}
}和
public static void CopyTo(Stream src, Stream dest)
{
byte[] bytes = new byte[4096];
int cnt;
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
{
dest.Write(bytes, 0, cnt);
}
}主要问题是,我不太确定如何在单元测试中通过由byte[]数据压缩的GZipStream。
正如我所相信的,在考试准备中,我不应该做这样的事情?这不就是破坏可测试性吗?
[Fact]
public void UnzipData_CalledWithByteArrayParameter_ReturnsString()
{
_serializationService.CallerName = "";
string someString = "fdfsdfsdfsdfsf";
byte[] array = _serializationService.ZipData(someString); //using compress method first????
string result = _serializationService.UnzipData(array);
Assert.False(result.Length == 0);
Assert.True(result.Length > array.Length);
}或者我可以?
ZipData方法:
public byte[] ZipData(string data)
{
LoadDictionaries();
CallerName = _messageService.GetCallerName();
var bytes = Encoding.UTF8.GetBytes(data);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
try
{
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
CopyTo(msi, gs);
}
byte[] toReturn = mso.ToArray();
return toReturn;
}
catch (Exception e)
{
_logger.Error(e, MessagesError[CallerName]);
return null;
}
}
}压缩方法返回给我:
new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 0, 11, 125, 206, 61, 11, 194, 48, 16, 6, 224, 255, 114, 171, 77, 185, 94, 62, 74, 179, 234, 234, 34, 221, 196, 33, 144, 27, 2, 53, 197, 38, 138, 80, 250, 223, 141, 90, 16, 151, 194, 45, 7, 247, 222, 243, 158, 103, 8, 79, 176, 77, 5, 251, 49, 102, 142, 249, 56, 122, 30, 192, 206, 16, 221, 149, 193, 194, 193, 69, 7, 21, 120, 151, 223, 27, 97, 99, 4, 106, 129, 166, 39, 178, 100, 202, 212, 168, 100, 139, 170, 219, 33, 189, 196, 245, 244, 196, 183, 59, 167, 204, 254, 47, 3, 203, 182, 125, 61, 218, 240, 166, 129, 35, 151, 63, 143, 144, 66, 78, 159, 110, 63, 93, 9, 236, 4, 233, 30, 181, 45, 5, 164, 172, 165, 54, 70, 181, 155, 250, 154, 41, 250, 229, 5, 222, 237, 15, 25, 239, 0, 0, 0 }
从字符串:
[{\"ix\":1,\"ContentModel\":{\"name\":\"Dana\",\"date\":\"2016-05-06T22:26:26.0437049+02:00\",\"dateRequested\":\"2016-05-06\"}},{\"ix\":2,\"ContentModel\":{\"name\":\"Darlene\",\"visits\":1,\"date\":\"2014-09-25T05:22:33.3566479+02:00\",\"dateRequested\":\"2014-09-25\"}}]
更新测试
[Fact]
public void UnzipData_CalledWithByteArrayParameter_ReturnsString()
{
//string _jsonExample i converted in txtwizard.net/compression
string _jsonExample = "[{\"ix\":1,\"ContentModel\":{\"name\":\"Dana\",\"date\":\"2016-05-06T22:26:26.0437049+02:00\",\"dateRequested\":\"2016-05-06\"}},{\"ix\":2,\"ContentModel\":{\"name\":\"Darlene\",\"visits\":1,\"date\":\"2014-09-25T05:22:33.3566479+02:00\",\"dateRequested\":\"2014-09-25\"}}]";
var base64String = "H4sIAAAAAAAA/4WOPwvCMBDFv0tWm3K9/CnNqquLdDMOgdwQqCmaKIL0uxu1irgUbjjevXfvt79bFm6WmaaybD3GTDFvR09DkcopuiOVzbKNi86y4vEuvxWERnNQHHSPaFCXqUGKFmS3AjQAX/uOThdKmfxfzrJpqj79uNR/HijS6+c1pJDTzPzLIzl0HFUPyhQkIWqhtJbtIs+ce/IcHmtZn30RAQAA";
byte[] array = Convert.FromBase64String(base64String);
_serializationService.CallerName = "";
string result = _serializationService.UnzipData(array);
Assert.Equals(result, _jsonExample); //not equal, added extra backslashes
Assert.Equal("SomeMethod", _serializationService.CallerName);
_messageServiceMock.Verify(m => m.GetCallerName(It.IsAny<string>()), Times.Once);
}发布于 2019-08-04 06:36:53
您的源数据如下:-
byte[] array = new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 0, 11, 117, 205, 49, 15, 130, 48, 16, 134, 255, 255, 114, 43, 148, 220, 157, 165, 45, 55, 234, 204, 98, 216, 140, 3, 9, 23, 37, 209, 18, 67, 53, 38, 134, 255, 46, 168, 139, 3, 243, 247, 189, 121, 14, 47, 232, 159, 32, 148, 195, 110, 136, 73, 99, 170, 135, 78, 47, 32, 47, 136, 237, 85, 65, 160, 214, 211, 185, 141, 144, 195, 163, 31, 251, 52, 126, 174, 93, 155, 150, 137, 145, 130, 65, 103, 152, 26, 44, 165, 172, 4, 109, 65, 155, 210, 57, 79, 25, 178, 32, 206, 213, 114, 221, 235, 237, 174, 99, 210, 238, 175, 129, 105, 202, 191, 56, 175, 226, 219, 97, 13, 174, 12, 178, 97, 219, 80, 16, 14, 98, 185, 32, 31, 188, 117, 62, 67, 90, 133, 127, 205, 12, 31, 223, 154, 207, 196, 62, 247, 0, 0, 0 };不是有效的Gzip数据,您的方法正在抛出一个返回null的InvalidDataException。
否则,您的方法可以很好地处理正确的输入数据。
发布于 2019-08-04 13:29:11
使用您提供的代码的核心部分来创建被测试主题的最小示例。
public class Subject {
public byte[] ZipData(string data) {
//LoadDictionaries();
//CallerName = _messageService.GetCallerName();
var bytes = Encoding.UTF8.GetBytes(data);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream()) {
try {
using (var gs = new GZipStream(mso, CompressionMode.Compress)) {
msi.CopyTo(gs);
}
byte[] toReturn = mso.ToArray();
return toReturn;
} catch (Exception e) {
// _logger.Error(e, MessagesError[CallerName]);
return Array.Empty<byte>();
}
}
}
public string UnzipData(byte[] bytes) {
//LoadDictionaries();
//CallerName = _messageService.GetCallerName();
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream()) {
try {
using (var gs = new GZipStream(msi, CompressionMode.Decompress)) {
gs.CopyTo(mso);
}
return Encoding.UTF8.GetString(mso.ToArray());
} catch (Exception e) {
//_logger.Error(e, MessagesError[CallerName]);
return string.Empty;
}
}
}
}下面的测试在执行时表现为预期的行为
[TestClass]
public class MyTestClass {
[TestMethod]
public void UnzipData_CalledWithByteArrayParameter_ReturnsString() {
//Arrange
var _serializationService = new Subject();
string expected = "[{\"ix\":1,\"ContentModel\":{\"name\":\"Dana\",\"date\":\"2016-05-06T22:26:26.0437049+02:00\",\"dateRequested\":\"2016-05-06\"}},{\"ix\":2,\"ContentModel\":{\"name\":\"Darlene\",\"visits\":1,\"date\":\"2014-09-25T05:22:33.3566479+02:00\",\"dateRequested\":\"2014-09-25\"}}]";
byte[] array = _serializationService.ZipData(expected);
//Act
string actual = _serializationService.UnzipData(array);
//Assert
actual.Should().NotBeNull()
.And.Be(expected);
}
}注意对使用的函数所做的更改。主要是不返回null,这会带来自身的并发症。
您的断言也是准确的,因为给定zip过程,字符串和字节数组长度将不匹配。
在我看来,压缩数据作为测试安排的一部分不会打破隔离,因为压缩不是正在测试的内容(技术上的)。
您可以轻松地在测试中再次手动重写邮政编码,以确保您有正确的数据提供给被测试的成员,但是为什么要重复现有的功能。
不会很干燥的。
这里的假设是zip功能也有自己的独立单元测试。如果其测试失败,则该成员已被覆盖。
https://stackoverflow.com/questions/57344184
复制相似问题