【问题标题】:xUnit Making tests for reading and writing to Json filexUnit 对 Json 文件进行读写测试
【发布时间】:2014-04-25 14:29:20
【问题描述】:

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

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

【问题讨论】:

    标签: c# asp.net json httpcontext nancy


    【解决方案1】:

    我建议您使用适配器或外观来抽象出使用 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。

    【讨论】:

    • 这正是我要找的,感谢您的回复。我会实施并告诉您进展如何,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多