当前位置: 首页>>代码示例>>C#>>正文


C# StringWriter.Stub方法代码示例

本文整理汇总了C#中System.IO.StringWriter.Stub方法的典型用法代码示例。如果您正苦于以下问题:C# StringWriter.Stub方法的具体用法?C# StringWriter.Stub怎么用?C# StringWriter.Stub使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.StringWriter的用法示例。


在下文中一共展示了StringWriter.Stub方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CommonConstructorArgsTest

        // Helper class to provide a common set of tests for constructor-args based
        // multi-mocks testing.  Exercises a mocked StringWriter (which should also be an IDataErrorInfo)
        // constructed with a mocked IFormatProvider.  The test checks the semantics
        // of the mocked StringWriter to compare it with the expected semantics.
        private static void CommonConstructorArgsTest(StringBuilder stringBuilder, IFormatProvider formatProvider, StringWriter mockedWriter, MockType mockType)
        {
            string stringToWrite = "The original string";
              string stringToWriteLine = "Extra bit";

              IDataErrorInfo errorInfo = mockedWriter as IDataErrorInfo;
              Assert.IsNotNull(errorInfo);

              // Configure expectations for mocked writer
              mockedWriter.Stub(x => x.FormatProvider).Return(formatProvider).CallOriginalMethod(OriginalCallOptions.CreateExpectation);
              mockedWriter.Expect(x => x.Write((string) null)).IgnoreArguments().CallOriginalMethod(OriginalCallOptions.CreateExpectation);
              mockedWriter.Expect(x => x.Flush()).Repeat.Any().CallOriginalMethod(OriginalCallOptions.CreateExpectation);
              mockedWriter.Expect(x => x.Close());

              // Configure expectations for object through interface
              errorInfo.Expect(x => x.Error).Return(null).Repeat.Once();
              errorInfo.Expect(x => x.Error).Return("error!!!").Repeat.Once();

              // Ensure that arguments arrived okay
              // Is the format provider correct
              Assert.AreSame(formatProvider, mockedWriter.FormatProvider, "FormatProvider");
              // Does writing to the writer forward to our stringbuilder from the constructor?
              mockedWriter.Write(stringToWrite);
              mockedWriter.Flush();

              // Let's see what mode our mock is running in.
              // We have not configured WriteLine at all, so:
              //  a) if we're running as a strict mock, it'll fail
              //  b) if we're running as a dynamic mock, it'll no-op
              //  c) if we're running as a partial mock, it'll work
              try
              {
            mockedWriter.WriteLine(stringToWriteLine);
              }
              catch (ExpectationViolationException)
              {
            // We're operating strictly.
            Assert.AreEqual(MockType.Strict, mockType);
              }

              string expectedStringBuilderContents = null;
              switch (mockType)
              {
            case MockType.Dynamic:
            case MockType.Strict:
              // The writeline won't have done anything
              expectedStringBuilderContents = stringToWrite;
              break;
            case MockType.Partial:
              // The writeline will have worked
              expectedStringBuilderContents = stringToWrite + stringToWriteLine + Environment.NewLine;
              break;
              }

              Assert.AreEqual(expectedStringBuilderContents, stringBuilder.ToString());

              // Satisfy expectations.
              mockedWriter.Close();
              Assert.IsNull(errorInfo.Error, "Error should be null");
              Assert.AreEqual("error!!!", errorInfo.Error, "Should have gotten an error");

              if (MockType.Strict != mockType)
            mockedWriter.VerifyAllExpectations();
        }
开发者ID:bcraytor,项目名称:rhino-mocks,代码行数:68,代码来源:MultiMocksWithAAA.cs


注:本文中的System.IO.StringWriter.Stub方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。