使用SetupGet和SetupSet模拟属性 - 这有效,但为什么呢?

Ric*_*ett 23 c# properties moq

使用Moq我Report TheReport { get; set; }在一个接口上模拟一个属性,ISessionData这样我就可以检查在这个属性上设置的值.

为了达到这个目的,我正在使用SetupGetSetupSet如下:

// class-level fields
protected Report _sessionReport;
protected Mock<ISessionData> SessionData { get; private set; }
Run Code Online (Sandbox Code Playgroud)

在我的设置方法中......

SessionData = new Mock<ISessionData>();

SessionData
    .SetupSet(s => s.TheReport = It.IsAny<Report>())
    .Callback<RDLDesigner.Common.Report>(r =>
    {
        _sessionReport = r;
        SessionData.SetupGet(s => s.TheReport).Returns(_sessionReport);
    });
Run Code Online (Sandbox Code Playgroud)

我在StackOverflow上找到了这种方法并且它可以工作,但我不明白为什么.我期待SetupGetSetupSet回调之外接听电话.

任何人都可以解释这种方法的工作方式和原因,以及它是否是模拟这种类型属性的最合适方式?

编辑

使用SessionData.SetupProperty(s => s.TheReport);也适用于我的场景,但我仍然对我的原始方法如何以及为何起作用的任何解释感兴趣.

Joh*_*ter 32

在调用SetupGet时使用回调的原因是_sessionReport引用是按值传递的,这意味着对Set方法的后续调用不会更新get方法返回的值.

更清楚地看看发生了什么.如果您按照以下方式设置了Mock: -

SessionData.SetupSet(s => s.Report = It.IsAny<Report>());
SessionData.SetupGet(s => s.Report).Returns(_report);
Run Code Online (Sandbox Code Playgroud)

在伪代码中,Mocked实现看起来有点像

public Report Report {
    set { }
    get { 
       // Copy of the value of the _report reference field in your test class
       return _reportCopy; 
    }  
}
Run Code Online (Sandbox Code Playgroud)

所以做这样的事情是行不通的: -

 ISessionData session = SessionData.Object
 Report report = new Report();
 session.Report = report;
 session.Report.ShouldEqual(report); //Fails
 _report.ShouldEqual(report); // Fails
Run Code Online (Sandbox Code Playgroud)

显然我们需要在Set方法中添加一些行为,所以我们就像这样设置Mock

SessionData.SetupSet(s => s.Report = It.IsAny<Report>())
           .Callback(s => _report = s);
SessionData.SetupGet(s => s.Report).Returns(_report);
Run Code Online (Sandbox Code Playgroud)

这导致Mocked实现看起来有点像

public Report Report {
    set {
       // Invokes delegate that sets the field on test class
    }
    get { 
       // Copy of the original value of the _report reference field
       // in your test class
       return _reportCopy; 
    }  
}
Run Code Online (Sandbox Code Playgroud)

然而,这会导致以下问题: -

  ISessionData session = SessionData.Object
  Report report = new Report();
  session.Report = report;
  _report.ShouldEqual(report); // Passes
  session.Report.ShouldEqual(report); //Fails!
Run Code Online (Sandbox Code Playgroud)

本质上,属性上的"get"方法仍然返回对_report指向的原始对象的引用,因为引用是通过值传递给SetupGet方法的.

因此,每次调用setter时,我们都需要更新报告getter返回的值,这会导致您的原始代码

SessionData
   .SetupSet(s => s.TheReport = It.IsAny<Report>())
   .Callback<RDLDesigner.Common.Report>(r => {
        _sessionReport = r;
        SessionData.SetupGet(s => s.TheReport).Returns(_sessionReport);
   });
Run Code Online (Sandbox Code Playgroud)

这可确保Get方法返回的值始终与先前对set方法的调用保持同步.并导致某些(功能上)表现如下: -

public Report Report {
    set {
       // Sets the field on the test class
       _reportCopy = value;
    }
    get { 
       // Copy of the value of the _report reference field in your test class
       return _reportCopy; 
    }  
}
Run Code Online (Sandbox Code Playgroud)


sto*_*toj 11

在SetupSet实现中放置SetupGet似乎非常/过于复杂.

让.Returns返回委托更容易,因此每次都会对其进行评估,而不是只返回引用的副本.

这样的事情在眼睛上更容易(并且应该表现得更好,因为你没有不断地重新定义吸气剂).

SessionData
   .SetupSet(s => s.TheReport = It.IsAny<Report>())
   .Callback<RDLDesigner.Common.Report>(r => _sessionReport = r);    
SessionData
   .SetupGet(s => s.TheReport).Returns(() => _sessionReport);
Run Code Online (Sandbox Code Playgroud)