xUnit对Json文件进行读写测试

本文关键字:读写 测试 文件 Json xUnit | 更新日期: 2023-09-27 18:00:02

我正在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;
        }
    }

任何建议都很好,谢谢。

xUnit对Json文件进行读写测试

我建议您使用适配器或facade来抽象,而不使用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。