Ejemplo n.º 1
0
        // 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();
        }