gpt4 book ai didi

c# - xUnit 进行 Json 文件读写测试

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:15 25 4
gpt4 key购买 nike

我正在 NancyFx 中创建一个新应用程序,我正在尝试进行测试驱动开发 (TDD) 因此,我想要开发的第一个功能是读取和写入 json 文件。现在我知道这很难做到,因为它涉及 httpContext,除非应用程序正在运行,否则我显然不能这样做。

我想做的是模拟这个,这样我就可以创建一个单元测试来读取和写入 json 文件。任何人都可以向我展示我如何执行此操作的示例并解释您是如何完成的吗?

我有两种方法,一种用于阅读,一种用于写作,如下所示:

ReadToJsonFile 方法:

public IEnumerable<T> ReadFile<T>(string fileName)
{
try
{
var readFile = File.ReadAllText(HttpContext.Current.Server.MapPath(fileName));
var list = JsonConvert.DeserializeObject<List<T>>(readFile);
return list;
}
catch (Exception ex)
{
_logger.LogException(ex);
return null;
}
}

WriteToJsonFile 方法:

public bool WriteFile<T>(string fileName, IEnumerable<T> list)
{
try
{
var listContent = JsonConvert.SerializeObject(list, Formatting.Indented);
File.WriteAllText(HttpContext.Current.Server.MapPath(fileName), listContent);
return true;
}
catch (Exception ex)
{
_logger.LogException(ex);
return false;
}
}

任何建议都会很棒,谢谢。

最佳答案

我建议您使用适配器或门面来抽象使用 System.IO 静态方法:

public class JsonWriter
{
private readonly IFileSystem _file;
private readonly HttpContextBase _httpContext;
private readonly ILog _logger;

public JsonWriter(
IFileSystem file,
HttpContextBase httpContext,
ILog logger)
{
_file = file;
_httpContext = httpContext;
_logger = logger;
}

public bool WriteFile<T>(string fileName, IEnumerable<T> list)
{
try
{
var listContent = JsonConvert.SerializeObject(list, Formatting.Indented);
_file.WriteAllText(_httpContext.Server.MapPath(fileName), listContent);
return true;
}
catch (Exception ex)
{
_logger.LogException(ex);
return false;
}
}

}

你需要这样的界面:

///<summary>Implementors are wrappers for static methods of System.IO.File and System.IO.Directory
///</summary>
public interface IFileSystem
{

///<summary>Creates a new file, writes the specified string to the file, then closes the file. If the file already exists, it is overwritten.
///</summary>
///<param name="path">The file to write to</param>
///<param name="contents">The string to write to the file</param>
void WriteAllText(string path, string contents);

///<summary>Creates a new file, writes the specified string to the file, then closes the file. If the file already exists, it is overwritten.
///</summary>
///<param name="path">The file to write to</param>
///<param name="contents">The string to write to the file</param>
///<param name="encoding">An <see cref="Encoding"/> object that represents the encoding to apply to the string</param>
void WriteAllText(string path, string contents, Encoding encoding);

}

像这样的实现:

///<summary>Replaces the static methods of System.IO.File
///</summary>
public class FileSystem : IFileSystem
{
///<summary>Creates a new file, writes the specified string to the file, then closes the file. If the file already exists, it is overwritten.
///</summary>
///<param name="path">The file to write to</param>
///<param name="contents">The string to write to the file</param>
public void WriteAllText(string path, string contents)
{
File.WriteAllText(path, contents);
}

///<summary>Creates a new file, writes the specified string to the file, then closes the file. If the file already exists, it is overwritten.
///</summary>
///<param name="path">The file to write to</param>
///<param name="contents">The string to write to the file</param>
///<param name="encoding">An <see cref="Encoding"/> object that represents the encoding to apply to the string</param>
public void WriteAllText(string path, string contents, Encoding encoding)
{
File.WriteAllText(path, contents, encoding);
}
}

现在您可以在单元测试中模拟文件方法和 HttpContext。

关于c# - xUnit 进行 Json 文件读写测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23295972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com