使用Prig模拟消息框

时间:2018-06-19 10:26:28

标签: c# mocking

我希望从System.Windows.Forms模拟MessageBox。 尽可能使用Prig。

如果可能的话,请添加设置步骤,因为这很难实现。

我按照github链接中描述的方式进行操作,因此在为mscorlib'添加Prig程序集后,然后在' Sysyem.windows.Form'中添加相同的内容,添加所需的内容两者的间接设置。在构建项目之后,有时我会在错误列表选项卡enter image description here

中收到以下消息

没有PMessageBox类。

如果我对Datetime类做同样的事情,那就完美了。

更新: 这就是我的System.Windows.Forms.v4.0.30319.v4.0.0.0.prig的样子:

<?xml version="1.0" encoding="utf-8"?>

       

  ========================== EXAMPLE 1 ==========================
  PM> $methods = Find-IndirectionTarget datetime get_Now
  PM> $methods

  Method
  ======
  System.DateTime get_Now()


  PM> $methods[0] | Get-IndirectionStubSetting | clip
  PM>

  ========================== EXAMPLE 2 ==========================
  PM> $methods = Find-IndirectionTarget datetime CompareTo
  PM> $methods

  Method
  ======
  Int32 CompareTo(System.Object)
  Int32 CompareTo(System.DateTime)


  PM> $methods[0] | Get-IndirectionStubSetting | clip
  PM>

  Then, paste the clipboard content to between the tags 'stubs'.

- &GT;   

<stubs>
  <!--<add name="NowGet" alias="NowGet">
    <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assembly="0" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
      <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">get_Now</Name>
      <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib</AssemblyName>
      <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime</ClassName>
      <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime get_Now()</Signature>
      <Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime get_Now()</Signature2>
      <MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
      <GenericArguments i:nil="true" xmlns="" />
    </RuntimeMethodInfo>
  </add>-->
  <!--<add name="CompareToObject" alias="CompareToObject">
    <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assembly="0" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
      <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">CompareTo</Name>
      <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">mscorlib</AssemblyName>
      <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.DateTime</ClassName>
      <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">Int32 CompareTo(System.Object)</Signature>
      <Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">System.Int32 CompareTo(System.Object)</Signature2>
      <MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
      <GenericArguments i:nil="true" xmlns="" />
    </RuntimeMethodInfo>
  </add>-->
  <!-- 
      PMessageBox.ShowString().Body = 
          text => 
          {   
              throw new NotImplementedException();
          };
  -->
  <add name="ShowString" alias="ShowString">
    <RuntimeMethodInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:FactoryType="MemberInfoSerializationHolder" z:Type="System.Reflection.MemberInfoSerializationHolder" z:Assembly="0" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/System.Reflection">
      <Name z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">Show</Name>
      <AssemblyName z:Id="3" z:Type="System.String" z:Assembly="0" xmlns="">System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyName>
      <ClassName z:Id="4" z:Type="System.String" z:Assembly="0" xmlns="">System.Windows.Forms.MessageBox</ClassName>
      <Signature z:Id="5" z:Type="System.String" z:Assembly="0" xmlns="">System.Windows.Forms.DialogResult Show(System.String)</Signature>
      <Signature2 z:Id="6" z:Type="System.String" z:Assembly="0" xmlns="">System.Windows.Forms.DialogResult Show(System.String)</Signature2>
      <MemberType z:Id="7" z:Type="System.Int32" z:Assembly="0" xmlns="">8</MemberType>
      <GenericArguments i:nil="true" xmlns="" />
    </RuntimeMethodInfo>
  </add>
</stubs>

更新: 它现在正在工作。 从mscorlib.v4.0.30319.v4.0.0.0.prig中删除了间接设置 加。链接https://github.com/urasandesu/Prig/issues/97

它仍然没有从错误列表中删除邮件,但不知何故我创建了类PMessageBox。 我之前已经完成了这些设置,不知道这次是否有效。 如果我发现了什么,我会更新。

1 个答案:

答案 0 :(得分:1)

参考this文章,您可以在Visual studio IDE

上执行此操作
 [Test]
 public void MessageBoxShow_should_be_callable_indirectly()
 {
    using (new IndirectionsContext())
   {
    // Arrange
    var mockMessageBox = new Mock<IndirectionFunc<string, DialogResult>>();
    mockMessageBox.Setup(_ => _(string.Empty)).Returns(DialogResult.OK);

    PMessageBox.ShowString().Body = mockMessageBox.Object;

    // Act
    MessageBox.Show("This is a message");

    // Assert
    mockMessageBox.Verify(_ => _("This is a message"));
  }
}
相关问题