c# - c# Moq objects void方法,改变objects参数值
问题描述
我正在尝试在我的单元测试中使用 Mocks,但我在下面的代码中遇到了困难。我想以正确的方式从 AppendName 方法回调,以便正确测试此方法并实际更改模拟对象名称。
public class GenericTests
{
[TestMethod]
public void TestMethod1()
{
IFile newFile = GetNewFile("Document").Object;
newFile.AppendName();
Assert.AreEqual("AppendedDocument", newFile.Name);
}
public Mock<IFile> GetNewFile(string name)
{
Mock<IFile> file = new Mock<IFile>();
file.Setup(f => f.Name).Returns(name);
file.Setup(f => f.Path).Returns(@"C:\Folder\" + name);
file.Setup(f => f.AppendName()).Callback.....problem is here.....
return file;
}
}
public interface IFile
{
string Name { get; set; }
string Path { get; set; }
void AppendName();
}
public class LocalFile : IFile
{
public string Name { get; set; }
public string Path { get; set; }
public void AppendName()
{
this.Name = "AppendedDocument";
}
}
有人可以建议如何完成 AppendName() 的回调吗?请
解决方案
你看,接口IFile
和类LocalFile
是不同的类型。所以我们不能从默认情况下Mock<IFile>
重现实现。LocalFile
为了使您的测试工作,您可以添加下一个Callback
调用:
public Mock<IFile> GetNewFile(string name)
{
Mock<IFile> file = new Mock<IFile>();
file.CallBase = true;
file.Setup(f => f.Name).Returns(name);
file.Setup(f => f.Path).Returns(@"C:\Folder\" + name);
file.Setup(f => f.AppendName()).Callback(() => file.Setup(f => f.Name).Returns(() =>
{
var localFile = new LocalFile();
localFile.AppendName();
return localFile.Name;
// or just return "AppendedDocument"
}));
return file;
}
示例有效,但是我想这不是您所需要的。即使对于Mock<LocalFile>
with CallBase = true
,public virtual public string Name { get; set; }
您也需要以某种方式清除属性设置。据我所知,起订量不允许这样做。我可能是错的。您可以基于相同的想法尝试下一个解决方法:
public class GenericTests
{
[Test]
public void TestMethod1()
{
IFile newFile = GetNewFile("Document");
newFile.AppendName();
Assert.AreEqual("AppendedDocument", newFile.Name);
}
public IFile GetNewFile(string name)
{
Mock<LocalFile> file = new Mock<LocalFile>();
file.CallBase = true;
file.Object.AppendName();
// remember expected result before setting up the mock
var appendDoc = file.Object.Name;
file.Setup(f => f.Name).Returns(name);
file.Setup(f => f.Path).Returns(@"C:\Folder\" + name);
file.Setup(f => f.AppendName())
// change property behavior after call of AppendName
.Callback(() => file.Setup(f => f.Name).Returns(appendDoc));
return file.Object;
}
}
public interface IFile
{
string Name { get; set; }
string Path { get; set; }
void AppendName();
}
public class LocalFile : IFile
{
public virtual string Name { get; set; }
public virtual string Path { get; set; }
public virtual void AppendName()
{
this.Name = "AppendedDocument";
}
}
我稍微改变了你的例子。GetNewFile
现在返回IFile
实例和LocalFile
成为虚拟的成员。希望能帮助到你。
推荐阅读
- c++ - 错误:对非类类型“unsigned int”的成员的请求
- bash - 从 CSV 文件在 yaml 中生成模板
- c# - C++ 到 C# ReadProcessMemory
- java - 测试整数是否可以创建三角形
- haskell - 为什么这个定点计算不停止?
- python - str.replace() 不能将零点 (0.) 替换为零点零 (0.0)
- mysql - 用插入和递归左连接填充缺失的小时数
- python - 引发自定义异常和输出
- xcode - 为什么我从 lex "gm4:stdin:1744: ERROR: end of file in string" 收到 Xcode 构建错误
- html - 删除a之间的间距和一个
标签