本文整理汇总了C#中System.IO.StringWriter.Expect方法的典型用法代码示例。如果您正苦于以下问题:C# StringWriter.Expect方法的具体用法?C# StringWriter.Expect怎么用?C# StringWriter.Expect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StringWriter
的用法示例。
在下文中一共展示了StringWriter.Expect方法的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();
}