c# - 使用 SetupGet 和 SetupSet 模拟属性 - 这可行,但为什么呢?

标签 c# properties moq

使用 Moq 我正在模拟一个属性,Report TheReport { get;放; 在接口(interface) ISessionData 上,以便我可以检查在此属性上设置的值。

为此,我使用了 SetupGetSetupSet,如下所示:

// class-level fields
protected Report _sessionReport;
protected Mock<ISessionData> SessionData { get; private set; }

在我的设置方法中...

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);
    });

我在 StackOverflow 上找到了这种方法它有效,但我不明白为什么。我希望在 SetupSet 回调之外调用 SetupGet

谁能解释这种方法的工作原理和原因,以及它是否是模拟此类属性的最合适方法?

编辑

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

最佳答案

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

为了更清楚地看到发生了什么。如果您按如下方式设置 Mock:-

SessionData.SetupSet(s => s.Report = It.IsAny<Report>());
SessionData.SetupGet(s => s.Report).Returns(_report);

在伪代码中,模拟的实现看起来有点像

public Report Report {
    set { }
    get { 
       // Copy of the value of the _report reference field in your test class
       return _reportCopy; 
    }  
}

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

 ISessionData session = SessionData.Object
 Report report = new Report();
 session.Report = report;
 session.Report.ShouldEqual(report); //Fails
 _report.ShouldEqual(report); // Fails

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

SessionData.SetupSet(s => s.Report = It.IsAny<Report>())
           .Callback(s => _report = s);
SessionData.SetupGet(s => s.Report).Returns(_report);

这导致 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; 
    }  
}

但是这会导致以下问题:-

  ISessionData session = SessionData.Object
  Report report = new Report();
  session.Report = report;
  _report.ShouldEqual(report); // Passes
  session.Report.ShouldEqual(report); //Fails!

本质上,属性上的“get”方法仍然返回对 _report 指向的原始对象的引用,因为引用是按值传递给 SetupGet 方法的。

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

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

这确保了 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; 
    }  
}

关于c# - 使用 SetupGet 和 SetupSet 模拟属性 - 这可行,但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3108323/

相关文章:

c# - 如何检查 ListView 项目是否被选中

Objective-C 属性属性最佳实践

c# - Moq 使用 ReturnsAsync 并修改 It.IsAny 输入参数

c# - 使用 Moq 将参数修改为 stub void 方法

c# - 使用 Moq 部分模拟类内部方法

c# - 如何优化具有大文件 I/O(读取、写入)和计算的例程?

c# - 使用c#将文件刻录到CD

c# - 找到最小数量的墙

ios - 冒险游戏 Sprite 套件中的界面

objective-c - UILabel 子类在 Objective-C 中显示为 UILabel