Testing a method called many times in moq

我只是一个虾纸丫 提交于 2020-03-01 04:53:05

问题


I have an interface like so:

Interface IWriteFile {
  string FileName {get;set;}
  void Open();
  void WriteData(string dataToWrite);
  void Close();
}

I want to test a class that will use this interface to populate a file. It will be calling WriteData a bunch of times and I just want to test the final output. Is there a way to introduce a new private field to the Mock object that will get appended to each time WriteData(Data) is called?

I really just want to see what the file will look like at the end of the day. Is there a better approach to this?


回答1:


So, do you need some sort of count of the number of times it is called?

int count = 0;
List<string> items = new List<string>();
var mock = new Mock<IWriteFile>();
mock.Setup(m => m.WriteData(It.IsAny<string>()))
    .Callback((string data) => { items.Add(data); count++; });


来源:https://stackoverflow.com/questions/3354937/testing-a-method-called-many-times-in-moq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!