在c#to单元测试中模拟json

在c#to单元测试中模拟json,c#,json,mocking,C#,Json,Mocking,我目前正试图对下面的代码进行单元测试,但我真的不知道该怎么做。读了一点之后,我想我需要使用mocking/faking并初始化一个假的json目录,我可以通过它进行单元测试,尽管mocking看起来非常复杂,我似乎无法理解它。这方面的任何帮助都将是惊人的。 提前感谢,, K 公共静态字符串ShowLocation=Directory.GetCurrentDirectory()++“\Database\Shows.txt”; 公共静态字符串GoldMemberLocation=Directory.

我目前正试图对下面的代码进行单元测试,但我真的不知道该怎么做。读了一点之后,我想我需要使用mocking/faking并初始化一个假的json目录,我可以通过它进行单元测试,尽管mocking看起来非常复杂,我似乎无法理解它。这方面的任何帮助都将是惊人的。 提前感谢,, K

公共静态字符串ShowLocation=Directory.GetCurrentDirectory()++“\Database\Shows.txt”;
公共静态字符串GoldMemberLocation=Directory.GetCurrentDirectory()++“\Database\GoldMembers.txt”;
/// 
///从数据库中删除特定节目。
/// 
/// 
公共静态无效删除显示(显示显示)
{
if(Directory.Exists(ShowLocation))
{
字符串json=File.ReadAllText(ShowLocation);
List data=JsonConvert.DeserializeObject(json);
数据删除(显示);
json=JsonConvert.SerializeObject(data.ToArray());
WriteAllText(ShowLocation,json);
}
其他的
{
File.WriteAllText(ShowLocation,String.Empty);
SaveShow(show);
}
}
/// 
///保存一个新的节目。
/// 
/// 
公共静态void SaveShow(Show-Show)
{
if(File.Exists(ShowLocation))
{
字符串json=File.ReadAllText(ShowLocation);
var data=新列表();
FileInfo f=新的FileInfo(ShowLocation);
如果(f.长度>0)
{
data=JsonConvert.DeserializeObject(json);
}
数据。添加(显示);
json=JsonConvert.SerializeObject(data.ToArray());
WriteAllText(ShowLocation,json);
}
其他的
{ 
var data=新列表();
数据。添加(显示);
var json=JsonConvert.SerializeObject(data.ToArray());
WriteAllText(ShowLocation,json);
}
}
/// 
///加载所有节目。
/// 
/// 
公共静态列表LoadShows()
{
if(File.Exists(ShowLocation))
{
字符串json=File.ReadAllText(ShowLocation);
List data=JsonConvert.DeserializeObject(json);
返回数据;
}
其他的
{
File.WriteAllText(ShowLocation,String.Empty);
返回新列表();
}
}
/// 
///删除特定的gold成员。
/// 
/// 
公共静态无效删除GoldMember(GoldMember GoldMember)
{
if(File.Exists(GoldMemberLocation))
{
字符串json=File.ReadAllText(GoldMemberLocation);
List data=JsonConvert.DeserializeObject(json);
数据删除(goldMember);
json=JsonConvert.SerializeObject(data.ToArray());
WriteAllText(GoldMemberLocation,json);
}
其他的
{
File.WriteAllText(GoldMemberLocation,String.Empty);
}
}
/// 
///保存一个新的黄金会员
/// 
/// 
公共静态void SaveGoldMembers(GoldMember GoldMember)
{
if(File.Exists(GoldMemberLocation))
{
字符串json=File.ReadAllText(GoldMemberLocation);
var data=新列表();
FileInfo f=新的FileInfo(GoldMemberLocation);
如果(f.长度>0)
{
data=JsonConvert.DeserializeObject(json);
}
添加数据(黄金会员);
json=JsonConvert.SerializeObject(data.ToArray());
WriteAllText(GoldMemberLocation,json);
}
其他的
{
var data=新列表();
添加数据(黄金会员);
var json=JsonConvert.SerializeObject(data.ToArray());
WriteAllText(GoldMemberLocation,json);
}
}

好的,是文件IO阻止了您的测试。因此,您需要将这些方法拉到一个接口,然后模拟该接口

然后设置希望这些模拟方法返回的内容

e、 g

//您将在运行时使用的具体实现。
公共类FileHelper:IFileHelper
{
bool DirectoryExists(字符串位置)=>Directory.Exists(位置);
string ReadAlltext(字符串位置)=>File.ReadAlltext(位置);
void writealText(字符串位置,字符串文本)=>File.writealText(位置,文本);
}
公共接口IFILHELPER
{
bool目录存在(字符串位置);
字符串ReadAlltext(字符串位置);
WriteAllText(字符串位置,字符串文本);
}
[测试方法]
公共void TestDeleteShow()
{
//创建FileHelper和安装程序的模拟
//方法以及您希望它们返回的内容。
Mock fileHelper=new Mock();
Setup(x=>x.DirectoryExists)。返回(true);
Setup(x=>x.ReadAllText)。返回(/*在此处放入一些文本*/);
字符串testLocation=“foo”;
Foo.DeleteShow(testLocation);
//验证是否按预期调用了模拟上的方法。
Verify(x=>x.writealText(It.IsAny(),string.Empty));
}
然后可以验证是否使用某些参数调用了某些方法。我的例子是使用Moq

e、 例如,您可以设置一个测试,其中DirectoryExists返回false,然后您可以验证File.WriteAllText是用空字符串和正确的位置调用的。等


上面的一条评论说这并没有测试任何逻辑。。。它的逻辑是,它可以被设置为测试,如果他传入一个不存在的目录,你可以断言一个文件是在那个位置创建的,它是空的。如果它确实存在,您可以断言它调用了该方法,但传入了正确的内容

这是该方法的唯一逻辑。。。当然,您可以测试File.writeAllines是否有效,但它是一个.net库方法。如果您正在测试.net framework,那么您就做错了。。类似的,不需要测试JSonConvert。。。。在
        public static string ShowLocation = Directory.GetCurrentDirectory() +  @"\Database\Shows.txt";
    public static string GoldMemberLocation = Directory.GetCurrentDirectory() + @"\Database\GoldMembers.txt";

    /// <summary>
    /// Deletes a specific show from the database.
    /// </summary>
    /// <param name="show"></param>
    public static void DeleteShow (Show show)
    {
        if (Directory.Exists(ShowLocation))
        {
            string json = File.ReadAllText(ShowLocation);

            List<Show> data = JsonConvert.DeserializeObject<List<Show>>(json);
            data.Remove(show);

            json = JsonConvert.SerializeObject(data.ToArray());

            File.WriteAllText(ShowLocation, json);
        }
        else
        {
            File.WriteAllText(ShowLocation, String.Empty);
            SaveShow(show);
        }
    }

    /// <summary>
    /// Saves a new Show.
    /// </summary>
    /// <param name="show"></param>
    public static void SaveShow(Show show)
    {
        if (File.Exists(ShowLocation))
        {
            string json = File.ReadAllText(ShowLocation);

            var data = new List<Show>();

            FileInfo f = new FileInfo(ShowLocation);
            if (f.Length > 0)
            {
                data = JsonConvert.DeserializeObject<List<Show>>(json);
            }

            data.Add(show);

            json = JsonConvert.SerializeObject(data.ToArray());

            File.WriteAllText(ShowLocation, json); 
        }
        else
        { 
            var data = new List<Show>();

            data.Add(show);

            var json = JsonConvert.SerializeObject(data.ToArray());

            File.WriteAllText(ShowLocation, json);
        }
    }

    /// <summary>
    /// Loads all the shows.
    /// </summary>
    /// <returns></returns>
    public static List<Show> LoadShows()
    {
        if (File.Exists(ShowLocation))
        {
            string json = File.ReadAllText(ShowLocation);

            List<Show> data = JsonConvert.DeserializeObject<List<Show>>(json);

            return data;
        }
        else
        {
            File.WriteAllText(ShowLocation, String.Empty);
            return new List<Show>();
        }

    }

    /// <summary>
    /// Deletes a specific gold member.
    /// </summary>
    /// <param name="goldMember"></param>
    public static void DeleteGoldMember(GoldMember goldMember)
    {
        if (File.Exists(GoldMemberLocation))
        {
            string json = File.ReadAllText(GoldMemberLocation);

            List<GoldMember> data = JsonConvert.DeserializeObject<List<GoldMember>>(json);
            data.Remove(goldMember);

            json = JsonConvert.SerializeObject(data.ToArray());

            File.WriteAllText(GoldMemberLocation, json);
        }
        else
        {
            File.WriteAllText(GoldMemberLocation, String.Empty);
        }
    }

    /// <summary>
    /// Saves a new Gold member
    /// </summary>
    /// <param name="goldMember"></param>
    public static void SaveGoldMembers (GoldMember goldMember)
    {

        if (File.Exists(GoldMemberLocation))
        {
            string json = File.ReadAllText(GoldMemberLocation);
            var data = new List<GoldMember>();

            FileInfo f = new FileInfo(GoldMemberLocation);
            if (f.Length > 0)
            {
                data = JsonConvert.DeserializeObject<List<GoldMember>>(json);
            }

            data.Add(goldMember);

            json = JsonConvert.SerializeObject(data.ToArray());

            File.WriteAllText(GoldMemberLocation, json);
        }
        else
        {
            var data = new List<GoldMember>();
            data.Add(goldMember);

            var json = JsonConvert.SerializeObject(data.ToArray());

            File.WriteAllText(GoldMemberLocation, json);
        }

    }
// A concrete implementation you will use at runtime.
public class FileHelper : IFileHelper
{
    bool DirectoryExists(string location) => Directory.Exists(location);
    string ReadAlltext(string location) => File.ReadAllText(location);
    void WriteAlLText(string location, string text) => File.WriteAlLText(location, text);
}

public interface IFileHelper
{
    bool DirectoryExists(string location);
    string ReadAlltext(string location);
    WriteAllText(string location, string text);
}
[TestMethod]
public void TestDeleteShow()
{
    // Create a mock of your FileHelper and setup
    // the methods and what you want them to return.
    Mock<IFileHelper> fileHelper = new Mock<IFileHelper>();
    fileHelper.Setup(x=>x.DirectoryExists).Returns(true);
    fileHelper.Setup(x=>x.ReadAllText).Returns( /* put some text in here */);
    string testLocation = "foo";
    Foo.DeleteShow(testLocation);

    // Verify the methods on your mock were called, as expected.
    fileHelper.Verify(x=>x.WriteAlLText(It.IsAny<string>(), string.Empty));
}