// 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.NotNull(errorInfo); // Configure expectations for mocked writer mockedWriter.Stub(x => x.FormatProvider).Return(formatProvider).CallOriginalMethod(); mockedWriter.Expect(x => x.Write((string)null)).IgnoreArguments().CallOriginalMethod(); mockedWriter.Expect(x => x.Flush()).Repeat.Any().CallOriginalMethod(); mockedWriter.Expect(x => x.Close()); // Configure expectations for object through interface errorInfo.Expect(x => x.Error).Return(null); errorInfo.Expect(x => x.Error).Return("error!!!"); // Ensure that arguments arrived okay // Is the format provider correct Assert.Same(formatProvider, mockedWriter.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.Equal(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.Equal(expectedStringBuilderContents, stringBuilder.ToString()); // Satisfy expectations. mockedWriter.Close(); Assert.Null(errorInfo.Error); Assert.Equal("error!!!", errorInfo.Error); if (MockType.Strict != mockType) { mockedWriter.VerifyAllExpectations(); } }