单元测试添加到 Zip/从 Zip 中提取
我有以下用于添加到 Zip 或从 Zip 中提取的代码。 我正在尝试重构它以使其做好测试准备。 有人可以指导我如何实现这一目标吗? 旁白:我使用 Moq 作为我的模拟框架,使用 MSTest 作为我的单元测试工具,
private const long BufferSize = 4096;
public static void ExtractZip(string zipFilename, string folder) {
using (var zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) {
foreach (var part in zip.GetParts()) {
using (var reader = new StreamReader(part.GetStream(FileMode.Open, FileAccess.Read))) {
using (var writer = new FileStream(folder + "\\" + Path.GetFileName(part.Uri.OriginalString),
FileMode.Create, FileAccess.Write)) {
var buffer = System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd());
writer.Write(buffer, 0, buffer.Length);
}
}
}
}
}
public static void AddFileToZip(string zipFilename, string fileToAdd) {
using (var zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) {
var destFilename = ".\\" + Path.GetFileName(fileToAdd);
var uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri)) {
zip.DeletePart(uri);
}
var part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (var fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read)) {
using (var dest = part.GetStream()) {
CopyStream(fileStream, dest);
}
}
}
}
提前致谢。
I have the following code for adding to/extracting from Zip. I'm trying to refactor this to make it test-ready. Can someone provide pointers on how I can accomplish this?
Aside: I'm using Moq as my mock framework and MSTest as my Unit Testing tool
private const long BufferSize = 4096;
public static void ExtractZip(string zipFilename, string folder) {
using (var zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) {
foreach (var part in zip.GetParts()) {
using (var reader = new StreamReader(part.GetStream(FileMode.Open, FileAccess.Read))) {
using (var writer = new FileStream(folder + "\\" + Path.GetFileName(part.Uri.OriginalString),
FileMode.Create, FileAccess.Write)) {
var buffer = System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd());
writer.Write(buffer, 0, buffer.Length);
}
}
}
}
}
public static void AddFileToZip(string zipFilename, string fileToAdd) {
using (var zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) {
var destFilename = ".\\" + Path.GetFileName(fileToAdd);
var uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri)) {
zip.DeletePart(uri);
}
var part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (var fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read)) {
using (var dest = part.GetStream()) {
CopyStream(fileStream, dest);
}
}
}
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将使这两个静态方法(ExtractZip 和 AddFileToZip)成为实例方法,并将其放入接口中:
客户端代码现在很容易测试。
Zipper 类怎么样?
I would make these two static methods (ExtractZip and AddFileToZip) instance methods, and put it into an interface:
Client code is now easy to test.
What about the Zipper class?
抽象出
IStreamProvider
后面FileStream
的创建,并将其传递给AddFileToZip
和ExtractZip
。 除非您愿意在进行单元测试时进行磁盘 IO,否则您必须抽象文件系统操作。Abstract away the creation of
FileStream
s behindIStreamProvider
and pass it toAddFileToZip
andExtractZip
. You'll have to abstract filesystem operations unless you're willing to do disk IO while doing unit tests.我将最终的代码粘贴在这里,因为它可能会对某人有所帮助,并且还可以让我获得反馈。
感谢斯特凡为我指明了正确的方向。
<代码> <代码>
>
I'm pasting the final code here as it might help someone and also allow me to get feedback.
Thanks to Stefan for pointing me in the right direction.