27

(如标签所示,我使用的是最小起订量)。

我有一个这样的界面:

interface ISource
{
  string Name { get; set; }
  int Id { get; set; }
}

interface IExample
{
  string Name { get; }
  ISource Source { get; set; }
}

在我的应用程序中,IExample 的具体实例接受 DTO (IDataTransferObject) 作为源。IExample 的具体实现上的一些属性只是简单地委托给 Source。像这样...

class Example : IExample
{
  IDataTransferObject Source { get; set; }

  string Name { get { return _data.Name; } }
}

我想创建一个独立的 IExample 模拟(独立意味着我不能使用捕获的变量,因为 IExample 模拟的多个实例将在测试过程中创建)并设置模拟使得 IExample.Name 返回值IExample.Source.Name。所以,我想创建一个像这样的模拟:

var example = new Mock<IExample>();
example.SetupProperty(ex => ex.Source);
example.SetupGet(ex => ex.Name).Returns(what can I put here to return ex.Source.Name);

本质上,我想将模拟配置为返回模拟的子对象的属性值作为一个属性的值。

谢谢。

4

1 回答 1

49

你可能会使用:

example.SetupGet(ex => ex.Name).Returns(() => example.Object.Source.Name);

当访问该属性时,将确定要返回的值,并将从Name模拟属性的Source属性中获取。

于 2012-07-18T16:06:01.550 回答