我有以下方法
public void ValidateAccessToFile(string filePath, List<String> errorMessageList)
{
try
{
using (FileStream fs = (FileStream)_iomgr.OpenFile(filePath))
{
if (fs.CanRead && fs.CanWrite) { }
else
{
errorMessageList.Add("Can not read/write to the specified file.");
}
}
}
catch (Exception e)
{
errorMessageList.Add(e.Message);
}
}
如果属性 CanRead 或 CanWrite 中的任何一个为假,我想测试它是否写入“errorMessageList”。这可能吗?
[Test]
public void ValidateAccessToFile_CanReadWriteToFile_NoErrorAddedToErrorListMessage()
{
Mock<IIOManager> mock = new Mock<IIOManager>();
mock.Setup(x => x.OpenFile(It.IsAny<string>())).Returns(new MemoryStream());
List<String> errorMessageList = new List<string>();
VerificationManager testObject = new VerificationManager(mock.Object);
testObject.ValidateAccessToFile("Random.txt", errorMessageList);
Assert.AreEqual(errorMessageList.Count, 0);
}
如何为 New MemoryStream 属性赋值?谢谢你的时间。
编辑
下面是我写的最终测试方法。
public void ValidateAccessToFile_CanReadWriteToFile_NoErrorAddedToErrorListMessage()
{
bool _isReadOnly = true;
List<String> errorMessageList = new List<string>();
Mock<IIOManager> mock = new Mock<IIOManager>();
mock
.Setup(x => x.IsReadOnly(It.IsAny<string>()))
.Returns(_isReadOnly);
VerificationManager testObject = new VerificationManager(mock.Object);
testObject.ValidateAccessToFile(It.IsAny<string>(), errorMessageList);
Assert.AreEqual(errorMessageList.Count, 0);
}