我在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;
}
}
任何建议都很棒,谢谢。
答案 0 :(得分:2)
我建议您使用适配器或外观来抽象使用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。