diff --git a/.gitignore b/.gitignore index b87a50311..bee604391 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ _ReSharper*/ *.userprefs *.swp *.DotSettings + +/packages diff --git a/.nuget/NuGet.Config b/.nuget/NuGet.Config new file mode 100644 index 000000000..67f8ea046 --- /dev/null +++ b/.nuget/NuGet.Config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.nuget/NuGet.targets b/.nuget/NuGet.targets new file mode 100644 index 000000000..46a1b6ced --- /dev/null +++ b/.nuget/NuGet.targets @@ -0,0 +1,133 @@ + + + + $(MSBuildProjectDirectory)\..\ + + + false + + + false + + + true + + + false + + + + + + + + + + + $([System.IO.Path]::Combine($(SolutionDir), ".nuget")) + $([System.IO.Path]::Combine($(ProjectDir), "packages.config")) + + + + + $(SolutionDir).nuget + packages.config + + + + + $(NuGetToolsPath)\NuGet.exe + @(PackageSource) + + "$(NuGetExePath)" + mono --runtime=v4.0.30319 $(NuGetExePath) + + $(TargetDir.Trim('\\')) + + -RequireConsent + -NonInteractive + + + $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir "$(SolutionDir) " + $(NuGetCommand) pack "$(ProjectPath)" -Properties Configuration=$(Configuration) $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols + + + + RestorePackages; + $(BuildDependsOn); + + + + + $(BuildDependsOn); + BuildPackage; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Source/Moq.csproj b/Source/Moq.csproj index 8d3f048d9..f41ee37cc 100644 --- a/Source/Moq.csproj +++ b/Source/Moq.csproj @@ -1,260 +1,264 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {1C91AC30-5977-4BE5-BA67-8EB186C03514} - Library - Properties - Moq - Moq - v4.0 - 512 - true - ..\Moq.snk - - - - true - full - false - bin\Debug\ - TRACE;DEBUG;DESKTOP - TRACE;DEBUG;DESKTOP;NET3x - prompt - 4 - bin\Debug\Moq.xml - true - AllRules.ruleset - false - - - - - full - true - bin\Release\ - TRACE;CODE_ANALYSIS - TRACE;CODE_ANALYSIS;NET3x - prompt - 4 - bin\Release\Moq.xml - true - true - true - true - AllRules.ruleset - - - - False - ..\Lib\Castle\bin-NET40\Castle.Core.dll - - - False - ..\Lib\Castle\bin-NET35\Castle.Core.dll - False - - - - - - - - - - True - True - ICallback.tt - - - - True - True - IRaise.tt - - - - True - True - IReturns.tt - - - - - - - - - - - - - - True - True - MethodCall.tt - - - True - True - MethodCallReturn.tt - - - True - True - Delegates.tt - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - True - True - Resources.resx - - - - - - - - - - - ResXFileCodeGenerator - Designer - Resources.Designer.cs - - - - - Moq.snk - - - IDefaultValueProvider.cs - - - - It.cs - - - TextTemplatingFileGenerator - ICallback.Generated.cs - - - TextTemplatingFileGenerator - IRaise.Generated.cs - - - TextTemplatingFileGenerator - IReturns.Generated.cs - - - Match.cs - - - TextTemplatingFileGenerator - MethodCallReturn.Generated.cs - - - TextTemplatingFileGenerator - Delegates.cs - - - Mock.Generic.cs - Designer - - - Mock.cs - - - - - - - - - - - - - - - - - - - - - - - - - - TextTemplatingFileGenerator - MethodCall.Generated.cs - - - Times.cs - - - - - - - - - - - - - - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {1C91AC30-5977-4BE5-BA67-8EB186C03514} + Library + Properties + Moq + Moq + v4.0 + 512 + true + ..\Moq.snk + + ..\ + true + + + true + full + false + bin\Debug\ + TRACE;DEBUG;DESKTOP + TRACE;DEBUG;DESKTOP;NET3x + prompt + 4 + bin\Debug\Moq.xml + true + AllRules.ruleset + false + + + + + full + true + bin\Release\ + TRACE;CODE_ANALYSIS + TRACE;CODE_ANALYSIS;NET3x + prompt + 4 + bin\Release\Moq.xml + true + true + true + true + AllRules.ruleset + + + + False + ..\packages\Castle.Core.3.2.0\lib\net40-client\Castle.Core.dll + + + False + ..\packages\Castle.Core.3.2.0\lib\net35\Castle.Core.dll + False + + + + + + + + + + True + True + ICallback.tt + + + + True + True + IRaise.tt + + + + True + True + IReturns.tt + + + + + + + + + + + + + + True + True + MethodCall.tt + + + True + True + MethodCallReturn.tt + + + True + True + Delegates.tt + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + True + True + Resources.resx + + + + + + + + + + + ResXFileCodeGenerator + Designer + Resources.Designer.cs + + + + + Moq.snk + + + IDefaultValueProvider.cs + + + + It.cs + + + TextTemplatingFileGenerator + ICallback.Generated.cs + + + TextTemplatingFileGenerator + IRaise.Generated.cs + + + TextTemplatingFileGenerator + IReturns.Generated.cs + + + Match.cs + + + TextTemplatingFileGenerator + MethodCallReturn.Generated.cs + + + TextTemplatingFileGenerator + Delegates.cs + + + Mock.Generic.cs + Designer + + + Mock.cs + + + + + + + + + + + + + + + + + + + + + + + + + + TextTemplatingFileGenerator + MethodCall.Generated.cs + + + + Times.cs + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Source/Properties/AssemblyInfo.cs b/Source/Properties/AssemblyInfo.cs index 3b07ce3f6..64f434d19 100644 --- a/Source/Properties/AssemblyInfo.cs +++ b/Source/Properties/AssemblyInfo.cs @@ -1,36 +1,35 @@ -using System; -using System.Reflection; -using System.Resources; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -[assembly: AssemblyTitle("Moq")] -[assembly: AssemblyDescription("Mocking library for .NET 3.5+ and C# 3.0+, heavily based on Linq")] -[assembly: AssemblyCompany("Clarius Consulting, Manas Technology Solutions, InSTEDD")] -[assembly: AssemblyProduct("Moq")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("4.0.10818.0")] -[assembly: AssemblyFileVersion("4.0.0.0")] - -[assembly: ComVisible(false)] -[assembly: NeutralResourcesLanguage("en")] - -#if SILVERLIGHT -[assembly: InternalsVisibleTo("Moq.Silverlight.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001009f7a95086500f8f66d892174803850fed9c22225c2ccfff21f39c8af8abfa5415b1664efd0d8e0a6f7f2513b1c11659bd84723dc7900c3d481b833a73a2bcf1ed94c16c4be64d54352c86956c89930444e9ac15124d3693e3f029818e8410f167399d6b995324b635e95353ba97bfab856abbaeb9b40c9b160070c6325e22ddc")] -#else -[assembly: InternalsVisibleTo("Moq.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001009f7a95086500f8f66d892174803850fed9c22225c2ccfff21f39c8af8abfa5415b1664efd0d8e0a6f7f2513b1c11659bd84723dc7900c3d481b833a73a2bcf1ed94c16c4be64d54352c86956c89930444e9ac15124d3693e3f029818e8410f167399d6b995324b635e95353ba97bfab856abbaeb9b40c9b160070c6325e22ddc")] -#endif -[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] - -[assembly: CLSCompliant(true)] - -/// -/// -/// -/// -/// -/// -internal static class ThisAssembly -{ +using System; +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Moq")] +[assembly: AssemblyCompany("Clarius Consulting, Manas Technology Solutions, InSTEDD")] +[assembly: AssemblyProduct("Moq")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +[assembly: AssemblyVersion("4.0.10818.0")] +[assembly: AssemblyFileVersion("4.0.0.0")] + +[assembly: ComVisible(false)] +[assembly: NeutralResourcesLanguage("en")] + +#if SILVERLIGHT +[assembly: InternalsVisibleTo("Moq.Silverlight.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001009f7a95086500f8f66d892174803850fed9c22225c2ccfff21f39c8af8abfa5415b1664efd0d8e0a6f7f2513b1c11659bd84723dc7900c3d481b833a73a2bcf1ed94c16c4be64d54352c86956c89930444e9ac15124d3693e3f029818e8410f167399d6b995324b635e95353ba97bfab856abbaeb9b40c9b160070c6325e22ddc")] +#else +[assembly: InternalsVisibleTo("Moq.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001009f7a95086500f8f66d892174803850fed9c22225c2ccfff21f39c8af8abfa5415b1664efd0d8e0a6f7f2513b1c11659bd84723dc7900c3d481b833a73a2bcf1ed94c16c4be64d54352c86956c89930444e9ac15124d3693e3f029818e8410f167399d6b995324b635e95353ba97bfab856abbaeb9b40c9b160070c6325e22ddc")] +#endif +[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] + +[assembly: CLSCompliant(true)] + +/// +/// +/// +/// +/// +/// +internal static class ThisAssembly +{ } \ No newline at end of file diff --git a/Source/packages.config b/Source/packages.config new file mode 100644 index 000000000..01442097e --- /dev/null +++ b/Source/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/UnitTests/MatcherAttributeFixture.cs b/UnitTests/MatcherAttributeFixture.cs index 7268d413b..1c573511b 100644 --- a/UnitTests/MatcherAttributeFixture.cs +++ b/UnitTests/MatcherAttributeFixture.cs @@ -1,264 +1,258 @@ -using System; -using Xunit; - -namespace Moq.Tests -{ - public class MatcherAttributeFixture - { - public interface IFoo - { - void Bar(string p); - } - - [Fact] - public void ShouldFindGenericMethodMatcher() - { - var foo = new Mock(); - - foo.Object.Bar("asdf"); - - foo.Verify(f => f.Bar(Any())); - } - -#pragma warning disable 618 - [Matcher] - public T Any() - { - return default(T); - } - -#pragma warning restore 618 - - public bool Any(T value) - { - return true; - } - - [Fact] - public void ShouldNotFindPrivateMethodMatcher() - { - var foo = new Mock(); - - foo.Object.Bar("asd"); - - Assert.Throws(() => foo.Verify(f => f.Bar(OddLength()))); - } - -#pragma warning disable 618 - [Matcher] - private static string OddLength() - { - return default(string); - } -#pragma warning restore 618 - - private static bool OddLength(string value) - { - return value.Length % 2 == 0; - } - - [Fact] - public void ShouldTranslateToUseMatcherImplementation() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(x => x.Bar(IsMagicString())); - IsMagicStringCalled = false; - mock.Object.Bar("magic"); - Assert.True(IsMagicStringCalled); - } - - [Fact] - //[ExpectedException] not used so IsMagicStringCalled can be verified - public void ShouldTranslateToUseMatcherImplementation2() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(x => x.Bar(IsMagicString())); - IsMagicStringCalled = false; - Exception expectedException = null; - try - { - mock.Object.Bar("no-magic"); - } - catch (Exception e) - { - expectedException = e; - } - - Assert.True(IsMagicStringCalled); - Assert.NotNull(expectedException); - } - - private static bool IsMagicStringCalled; - -#pragma warning disable 618 - [Matcher] - public static string IsMagicString() - { - return null; - } -#pragma warning restore 618 - - public static bool IsMagicString(string arg) - { - IsMagicStringCalled = true; - return arg == "magic"; - } - - [Fact] - public void ShouldUseAditionalArguments() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(x => x.Bar(StartsWith("ma"))); - mock.Object.Bar("magic"); - } - - [Fact] - public void ShouldUseAditionalArguments2() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(x => x.Bar(StartsWith("ma"))); - Assert.Throws(() => mock.Object.Bar("no-magic")); - } - -#pragma warning disable 618 - [Matcher] - public static string StartsWith(string prefix) - { - return null; - } -#pragma warning restore 618 - - public static bool StartsWith(string arg, string prefix) - { - return arg.StartsWith(prefix); - } - - [Fact] - public void ExpectMissingMatcherMethod() - { - var mock = new Mock(MockBehavior.Strict); - - Assert.Throws( - "public static bool MatcherHookWithoutMatcherMethod(System.String) in class Moq.Tests.MatcherAttributeFixture.", - () => mock.Setup(x => x.Bar(MatcherHookWithoutMatcherMethod()))); - } - -#pragma warning disable 618 - [Matcher] - public static string MatcherHookWithoutMatcherMethod() - { - return null; - } -#pragma warning restore 618 - - [Fact] - public void ExpectMissingMatcherWithArgsMethod() - { - var mock = new Mock(MockBehavior.Strict); - - Assert.Throws( - "public static bool MatcherHook2WithoutMatcherMethod(System.String, System.Int32) in class Moq.Tests.MatcherAttributeFixture.", - () => mock.Setup(x => x.Bar(MatcherHook2WithoutMatcherMethod(6)))); - } - -#pragma warning disable 618 - [Matcher] - public static string MatcherHook2WithoutMatcherMethod(int a) - { - return null; - } -#pragma warning restore 618 - - [Fact] - public void UseCurrentInstanceAsContext() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(x => x.Bar(NonStaticMatcherHook())); - NonStaticMatcherHookExpectedArg = "Do It"; - - mock.Object.Bar("Do It"); - } - -#pragma warning disable 618 - [Matcher] - public string NonStaticMatcherHook() - { - return null; - } -#pragma warning restore 618 - - public bool NonStaticMatcherHook(string arg) - { - return arg == NonStaticMatcherHookExpectedArg; - } - - private string NonStaticMatcherHookExpectedArg; - - [Fact] - public void ExpectMissingNonStaticMatcherMethod() - { - var mock = new Mock(MockBehavior.Strict); - - Assert.Throws( - "public bool NonStaticMatcherHookWithoutMatcherMethod(System.String) in class Moq.Tests.MatcherAttributeFixture.", - () => mock.Setup(x => x.Bar(NonStaticMatcherHookWithoutMatcherMethod()))); - } - -#pragma warning disable 618 - [Matcher] - public string NonStaticMatcherHookWithoutMatcherMethod() - { - return null; - } -#pragma warning restore 618 - - [Fact] - public void AllowStaticMethodsInHelperClassAsMatcherHook() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(x => x.Bar(A.NotNull())); - mock.Object.Bar("a"); - } - - public static class A - { -#pragma warning disable 618 - [Matcher] - public static string NotNull() - { - return null; - } -#pragma warning restore 618 - - public static bool NotNull(string arg) - { - return arg != null; - } - } - - [Fact] - public void AllowHelperClassInstance() - { - var mock = new Mock(MockBehavior.Strict); - var b = new B(); - mock.Setup(x => x.Bar(b.NotNull())); - mock.Object.Bar("a"); - } - - public class B - { -#pragma warning disable 618 - [Matcher] - public string NotNull() - { - return null; - } -#pragma warning restore 618 - - public bool NotNull(string arg) - { - return arg != null; - } - } - } -} +using System; +using Xunit; + +namespace Moq.Tests +{ + public class MatcherAttributeFixture + { + public interface IFoo + { + void Bar(string p); + } + + [Fact] + public void ShouldFindGenericMethodMatcher() + { + var foo = new Mock(); + + foo.Object.Bar("asdf"); + + foo.Verify(f => f.Bar(Any())); + } + +#pragma warning disable 618 + [Matcher] + public T Any() + { + return default(T); + } + +#pragma warning restore 618 + + public bool Any(T value) + { + return true; + } + + [Fact] + public void ShouldNotFindPrivateMethodMatcher() + { + var foo = new Mock(); + + foo.Object.Bar("asd"); + + Assert.Throws(() => foo.Verify(f => f.Bar(OddLength()))); + } + +#pragma warning disable 618 + [Matcher] + private static string OddLength() + { + return default(string); + } +#pragma warning restore 618 + + private static bool OddLength(string value) + { + return value.Length % 2 == 0; + } + + [Fact] + public void ShouldTranslateToUseMatcherImplementation() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(x => x.Bar(IsMagicString())); + IsMagicStringCalled = false; + mock.Object.Bar("magic"); + Assert.True(IsMagicStringCalled); + } + + [Fact] + //[ExpectedException] not used so IsMagicStringCalled can be verified + public void ShouldTranslateToUseMatcherImplementation2() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(x => x.Bar(IsMagicString())); + IsMagicStringCalled = false; + Exception expectedException = null; + try + { + mock.Object.Bar("no-magic"); + } + catch (Exception e) + { + expectedException = e; + } + + Assert.True(IsMagicStringCalled); + Assert.NotNull(expectedException); + } + + private static bool IsMagicStringCalled; + +#pragma warning disable 618 + [Matcher] + public static string IsMagicString() + { + return null; + } +#pragma warning restore 618 + + public static bool IsMagicString(string arg) + { + IsMagicStringCalled = true; + return arg == "magic"; + } + + [Fact] + public void ShouldUseAditionalArguments() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(x => x.Bar(StartsWith("ma"))); + mock.Object.Bar("magic"); + } + + [Fact] + public void ShouldUseAditionalArguments2() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(x => x.Bar(StartsWith("ma"))); + Assert.Throws(() => mock.Object.Bar("no-magic")); + } + +#pragma warning disable 618 + [Matcher] + public static string StartsWith(string prefix) + { + return null; + } +#pragma warning restore 618 + + public static bool StartsWith(string arg, string prefix) + { + return arg.StartsWith(prefix); + } + + [Fact] + public void ExpectMissingMatcherMethod() + { + var mock = new Mock(MockBehavior.Strict); + + Assert.Throws(() => mock.Setup(x => x.Bar(MatcherHookWithoutMatcherMethod()))); + } + +#pragma warning disable 618 + [Matcher] + public static string MatcherHookWithoutMatcherMethod() + { + return null; + } +#pragma warning restore 618 + + [Fact] + public void ExpectMissingMatcherWithArgsMethod() + { + var mock = new Mock(MockBehavior.Strict); + + Assert.Throws(() => mock.Setup(x => x.Bar(MatcherHook2WithoutMatcherMethod(6)))); + } + +#pragma warning disable 618 + [Matcher] + public static string MatcherHook2WithoutMatcherMethod(int a) + { + return null; + } +#pragma warning restore 618 + + [Fact] + public void UseCurrentInstanceAsContext() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(x => x.Bar(NonStaticMatcherHook())); + NonStaticMatcherHookExpectedArg = "Do It"; + + mock.Object.Bar("Do It"); + } + +#pragma warning disable 618 + [Matcher] + public string NonStaticMatcherHook() + { + return null; + } +#pragma warning restore 618 + + public bool NonStaticMatcherHook(string arg) + { + return arg == NonStaticMatcherHookExpectedArg; + } + + private string NonStaticMatcherHookExpectedArg; + + [Fact] + public void ExpectMissingNonStaticMatcherMethod() + { + var mock = new Mock(MockBehavior.Strict); + + Assert.Throws(() => mock.Setup(x => x.Bar(NonStaticMatcherHookWithoutMatcherMethod()))); + } + +#pragma warning disable 618 + [Matcher] + public string NonStaticMatcherHookWithoutMatcherMethod() + { + return null; + } +#pragma warning restore 618 + + [Fact] + public void AllowStaticMethodsInHelperClassAsMatcherHook() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(x => x.Bar(A.NotNull())); + mock.Object.Bar("a"); + } + + public static class A + { +#pragma warning disable 618 + [Matcher] + public static string NotNull() + { + return null; + } +#pragma warning restore 618 + + public static bool NotNull(string arg) + { + return arg != null; + } + } + + [Fact] + public void AllowHelperClassInstance() + { + var mock = new Mock(MockBehavior.Strict); + var b = new B(); + mock.Setup(x => x.Bar(b.NotNull())); + mock.Object.Bar("a"); + } + + public class B + { +#pragma warning disable 618 + [Matcher] + public string NotNull() + { + return null; + } +#pragma warning restore 618 + + public bool NotNull(string arg) + { + return arg != null; + } + } + } +} diff --git a/UnitTests/MockedFixture.cs b/UnitTests/MockedFixture.cs index ede7daeab..6fba64576 100644 --- a/UnitTests/MockedFixture.cs +++ b/UnitTests/MockedFixture.cs @@ -1,97 +1,95 @@ -using System; -using Xunit; - -namespace Moq.Tests -{ - public class MockedFixture - { - [Fact] - public void InterfaceMockedShouldImplementMocked() - { - Mock mock = new Mock(); - IFoo mocked = mock.Object; - Assert.True(mocked is IMocked); - } - - [Fact] - public void MockOfMockedInterfaceShouldReturnSame() - { - Mock mock = new Mock(); - IMocked mocked = mock.Object as IMocked; - Assert.Same(mock, mocked.Mock); - } - - [Fact] - public void ClassMockedShouldImplementMocked() - { - Mock mock = new Mock(); - Foo mocked = mock.Object; - Assert.True(mocked is IMocked); - } - - [Fact] - public void MockOfMockedClassShouldReturnSame() - { - Mock mock = new Mock(); - IMocked mocked = mock.Object as IMocked; - Assert.Same(mock, mocked.Mock); - } - - public class FooWithCtor - { - public FooWithCtor(int a) { } - } - - [Fact] - public void ClassWithCtorMockedShouldImplementMocked() - { - Mock mock = new Mock(5); - FooWithCtor mocked = mock.Object; - Assert.True(mocked is IMocked); - } - - [Fact] - public void MockOfMockedClassWithCtorShouldReturnSame() - { - Mock mock = new Mock(5); - IMocked mocked = mock.Object as IMocked; - Assert.Same(mock, mocked.Mock); - } - - [Fact] - public void GetReturnsMockForAMocked() - { - var mock = new Mock(); - var mocked = mock.Object; - Assert.Same(mock, Mock.Get(mocked)); - } - - [Fact] - public void GetReturnsMockForAMockedAbstract() - { - var mock = new Mock(); - var mocked = mock.Object; - Assert.Same(mock, Mock.Get(mocked)); - } - - [Fact] - public void GetThrowsIfObjectIsNotMocked() - { - Assert.Throws( - "Object instance was not created by Moq.\r\nParameter name: mocked", - () => Mock.Get("foo")); - } - - public class FooBase - { - } - - public class Foo : FooBase - { - } - - public interface IFoo - { - } - } +using System; +using Xunit; + +namespace Moq.Tests +{ + public class MockedFixture + { + [Fact] + public void InterfaceMockedShouldImplementMocked() + { + Mock mock = new Mock(); + IFoo mocked = mock.Object; + Assert.True(mocked is IMocked); + } + + [Fact] + public void MockOfMockedInterfaceShouldReturnSame() + { + Mock mock = new Mock(); + IMocked mocked = mock.Object as IMocked; + Assert.Same(mock, mocked.Mock); + } + + [Fact] + public void ClassMockedShouldImplementMocked() + { + Mock mock = new Mock(); + Foo mocked = mock.Object; + Assert.True(mocked is IMocked); + } + + [Fact] + public void MockOfMockedClassShouldReturnSame() + { + Mock mock = new Mock(); + IMocked mocked = mock.Object as IMocked; + Assert.Same(mock, mocked.Mock); + } + + public class FooWithCtor + { + public FooWithCtor(int a) { } + } + + [Fact] + public void ClassWithCtorMockedShouldImplementMocked() + { + Mock mock = new Mock(5); + FooWithCtor mocked = mock.Object; + Assert.True(mocked is IMocked); + } + + [Fact] + public void MockOfMockedClassWithCtorShouldReturnSame() + { + Mock mock = new Mock(5); + IMocked mocked = mock.Object as IMocked; + Assert.Same(mock, mocked.Mock); + } + + [Fact] + public void GetReturnsMockForAMocked() + { + var mock = new Mock(); + var mocked = mock.Object; + Assert.Same(mock, Mock.Get(mocked)); + } + + [Fact] + public void GetReturnsMockForAMockedAbstract() + { + var mock = new Mock(); + var mocked = mock.Object; + Assert.Same(mock, Mock.Get(mocked)); + } + + [Fact] + public void GetThrowsIfObjectIsNotMocked() + { + Assert.Throws(() => Mock.Get("foo")); + } + + public class FooBase + { + } + + public class Foo : FooBase + { + } + + public interface IFoo + { + } + } } \ No newline at end of file diff --git a/UnitTests/Moq.Tests.csproj b/UnitTests/Moq.Tests.csproj index b63aca7c4..c98659b7e 100644 --- a/UnitTests/Moq.Tests.csproj +++ b/UnitTests/Moq.Tests.csproj @@ -1,166 +1,170 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {81BBC911-4916-4E10-A955-752AE47CB2B9} - Library - Properties - Moq.Tests - Moq.Tests - v4.0 - 512 - true - ..\Moq.snk - - - - true - full - false - bin\Debug\ - TRACE;DEBUG;DESKTOP - $(DefineConstants);NET3x - prompt - 4 - AllRules.ruleset - - - full - true - bin\Release\ - TRACE;DESKTOP - $(DefineConstants);NET3x - prompt - 4 - true - AllRules.ruleset - - - - False - ..\Lib\Castle\bin-NET40\Castle.Core.dll - - - False - ..\Lib\Castle\bin-NET35\Castle.Core.dll - - - .\ClassLibrary1.dll - - - .\ClassLibrary2.dll - - - False - False - c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\EnvDTE.dll - - - - 3.5 - - - 3.0 - - - 3.5 - - - - - False - ..\Lib\xUnit\xunit.dll - True - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Moq.snk - - - - - {1C91AC30-5977-4BE5-BA67-8EB186C03514} - Moq - - - - - {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52} - 2 - 5 - 0 - primary - False - True - - - {00020905-0000-0000-C000-000000000046} - 8 - 5 - 0 - primary - False - True - - - {0002E157-0000-0000-C000-000000000046} - 5 - 3 - 0 - primary - False - True - - - - - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {81BBC911-4916-4E10-A955-752AE47CB2B9} + Library + Properties + Moq.Tests + Moq.Tests + v4.0 + 512 + true + ..\Moq.snk + + ..\ + true + + + true + full + false + bin\Debug\ + TRACE;DEBUG;DESKTOP + $(DefineConstants);NET3x + prompt + 4 + AllRules.ruleset + + + full + true + bin\Release\ + TRACE;DESKTOP + $(DefineConstants);NET3x + prompt + 4 + true + AllRules.ruleset + + + + False + ..\packages\Castle.Core.3.2.0\lib\net40-client\Castle.Core.dll + + + False + ..\packages\Castle.Core.3.2.0\lib\net35\Castle.Core.dll + False + + + .\ClassLibrary1.dll + + + .\ClassLibrary2.dll + + + False + False + c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\EnvDTE.dll + + + + 3.5 + + + 3.0 + + + 3.5 + + + + + False + ..\packages\xunit.1.9.1\lib\net20\xunit.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Moq.snk + + + + + + {1C91AC30-5977-4BE5-BA67-8EB186C03514} + Moq + + + + + {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52} + 2 + 5 + 0 + primary + False + True + + + {00020905-0000-0000-C000-000000000046} + 8 + 5 + 0 + primary + False + True + + + {0002E157-0000-0000-C000-000000000046} + 5 + 3 + 0 + primary + False + True + + + + + + + + + + + \ No newline at end of file diff --git a/UnitTests/Regressions/IssueReportsFixture.cs b/UnitTests/Regressions/IssueReportsFixture.cs index ce6865585..657785267 100644 --- a/UnitTests/Regressions/IssueReportsFixture.cs +++ b/UnitTests/Regressions/IssueReportsFixture.cs @@ -1,1772 +1,1770 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.IO; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -using System.Text; -using Moq; -using Moq.Properties; -using Moq.Protected; -using Xunit; - -#if !SILVERLIGHT -using System.ServiceModel; -using System.ServiceModel.Web; -using System.Web.UI.HtmlControls; -#endif - -#region #181 - -// NOTE class without namespace -public class _181 -{ - [Fact] - public void ReproTest() - { - var mock = new Mock(); - mock.Object.Dispose(); - - mock.Verify(d => d.Dispose()); - } -} - -#endregion - -namespace Moq.Tests.Regressions -{ - public class IssueReportsFixture - { - #region #47 - - [Fact] - public void ShouldReturnListFromDateTimeArg() - { - var items = new List() { "Foo", "Bar" }; - - var mock = new Mock(MockBehavior.Strict); - mock - .Setup(m => m.GetValuesSince(It.IsAny())) - .Returns(items); - - var actual = mock.Object.GetValuesSince(DateTime.Now).ToList(); - - Assert.Equal(items.Count, actual.Count); - } - - public interface IMyClass - { - IEnumerable GetValuesSince(DateTime since); - } - - #endregion - - #region #48 - - public class Issue48 - { - [Fact] - public void ExpectsOnIndexer() - { - var mock = new Mock(); - mock.Setup(m => m[0]).Returns("a"); - mock.Setup(m => m[1]).Returns("b"); - - Assert.Equal("a", mock.Object[0]); - Assert.Equal("b", mock.Object[1]); - Assert.Equal(default(string), mock.Object[2]); - } - - public interface ISomeInterface - { - string this[int index] { get; set; } - } - } - - #endregion - - #region #52 - - [Fact] - public void ShouldNotOverridePreviousExpectation() - { - var ids = Enumerable.Range(1, 10); - var mock = new Mock(MockBehavior.Strict); - - foreach (var id in ids) - { - mock.Setup(x => x.DoSomething(id)); - } - - var component = mock.Object; - - foreach (var id in ids) - { - component.DoSomething(id); - } - } - - public interface IOverwritingMethod - { - void DoSomething(int id); - } - - #endregion - - #region #62 - - public interface ISomething - { - void DoSomething() where U : T; - } - - [Fact] - public void CreatesMockWithGenericsConstraints() - { - var mock = new Mock>(); - } - - #endregion - - #region #60 - - public interface IFoo - { - void DoThings(object arg); - } - - [Fact] - public void TwoExpectations() - { - Mock mocked = new Mock(MockBehavior.Strict); - object arg1 = new object(); - object arg2 = new object(); - - mocked.Setup(m => m.DoThings(arg1)); - mocked.Setup(m => m.DoThings(arg2)); - - mocked.Object.DoThings(arg1); - mocked.Object.DoThings(arg2); - - mocked.VerifyAll(); - } - - #endregion - - #region #21 - - [Fact] - public void MatchesLatestExpectations() - { - var mock = new Mock(); - - mock.Setup(m => m.Method(It.IsAny())).Returns(0); - mock.Setup(m => m.Method(It.IsInRange(0, 20, Range.Inclusive))).Returns(1); - - mock.Setup(m => m.Method(5)).Returns(2); - mock.Setup(m => m.Method(10)).Returns(3); - - Assert.Equal(3, mock.Object.Method(10)); - Assert.Equal(2, mock.Object.Method(5)); - Assert.Equal(1, mock.Object.Method(6)); - Assert.Equal(0, mock.Object.Method(25)); - } - - public interface IEvaluateLatest - { - int Method(int value); - } - - #endregion - - #region #49 - - [Fact] - public void UsesCustomMatchersWithGenerics() - { - var mock = new Mock(); - - mock.Setup(e => e.Method(IsEqual.To(5))).Returns(1); - mock.Setup(e => e.Method(IsEqual.To(6, "foo"))).Returns(2); - - Assert.Equal(1, mock.Object.Method(5)); - Assert.Equal(2, mock.Object.Method(6)); - } - - public static class IsEqual - { -#pragma warning disable 618 - [Matcher] - public static T To(T value) - { - return value; - } -#pragma warning restore 618 - - public static bool To(T left, T right) - { - return left.Equals(right); - } - -#pragma warning disable 618 - [Matcher] - public static T To(T value, U value2) - { - return value; - } -#pragma warning restore 618 - - public static bool To(T left, T right, U value) - { - return left.Equals(right); - } - } - - #endregion - - #region #68 - - [Fact] - public void GetMockCastedToObjectThrows() - { - var mock = new Mock(); - object m = mock.Object; - - Assert.Throws(() => Mock.Get(m)); - } - - #endregion - - #region #69 - - public interface IFooPtr - { - IntPtr Get(string input); - } - - [Fact] - public void ReturnsIntPtr() - { - Mock mock = new Mock(MockBehavior.Strict); - IntPtr ret = new IntPtr(3); - - mock.Setup(m => m.Get("a")).Returns(ret); - - IntPtr ret3 = mock.Object.Get("a"); - - Assert.Equal(ret, mock.Object.Get("a")); - } - - - #endregion - - #region #85 - - public class Issue85 - { - [Fact] - public void FooTest() - { - // Setup - var fooMock = new Mock(); - fooMock.CallBase = true; - fooMock.Setup(o => o.GetBar()).Returns(new Bar()); - var bar = ((IFoolery)fooMock.Object).DoStuffToBar(); - Assert.NotNull(bar); - } - - public interface IFoolery - { - Bar DoStuffToBar(); - } - - public class Foo : IFoolery - { - public virtual Bar GetBar() - { - return new Bar(); - } - - Bar IFoolery.DoStuffToBar() - { - return DoWeirdStuffToBar(); - } - - protected internal virtual Bar DoWeirdStuffToBar() - { - var bar = GetBar(); - //Would do stuff here. - return bar; - } - } - - public class Bar - { - } - } - - #endregion - - #region #89 - - public class Issue89 - { - [Fact] - public void That_last_expectation_should_win() - { - var mock = new Mock(); - mock.Setup(s => s.Get(1)).Returns("blah"); - mock.Setup(s => s.Get(It.IsAny())).Returns("foo"); - mock.Setup(s => s.Get(1)).Returns("bar"); - Assert.Equal("bar", mock.Object.Get(1)); - } - - public interface ISample - { - string Get(int i); - } - } - - #endregion - - #region #134 - - public class Issue134 - { - [Fact] - public void Test() - { - var target = new Mock(); - target.Setup(t => t.Submit(It.IsAny(), It.IsAny(), It.IsAny())); - - var e = Assert.Throws(() => target.VerifyAll()); - - Assert.Contains( - "IFoo t => t.Submit(It.IsAny(), It.IsAny(), new[] { It.IsAny() })", - e.Message); - } - - public interface IFoo - { - void Submit(string mailServer, string from, params int[] toRecipient); - } - } - - #endregion - - #region #136 - - public class _136 - { - // Fixed on PropertiesFixture.cs - } - - #endregion - - #region #138 - - public class _138 - { - public interface SuperFoo - { - string Bar { get; set; } - } - public interface Foo : SuperFoo - { - string Baz { get; set; } - } - - [Fact] - public void superFooMockSetupAllProperties() - { - var repo = new MockRepository(MockBehavior.Default); - var superFooMock = repo.Create(); - superFooMock.SetupAllProperties(); - - var superFoo = superFooMock.Object; - superFoo.Bar = "Bar"; - Assert.Equal("Bar", superFoo.Bar); - } - } - - #endregion - - #region #145 - - public class _145 - { - public interface IResolver - { - string Resolve(); - } - - public class DataWriter - { - } - - public class DataA { } - public class DataB { } - - [Fact] - public void ShouldDifferentiateBetweenGenericsParams() - { - var mock = new Mock(); - mock.Setup(m => m.Resolve>()).Returns("Success A"); - - Assert.Equal("Success A", mock.Object.Resolve>()); - - mock.Setup(m => m.Resolve>()).Returns("Success B"); - - Assert.Equal("Success B", mock.Object.Resolve>()); - Assert.Equal("Success A", mock.Object.Resolve>()); - } - - } - - #endregion - - #region #111 & #155 - - public class _111 - { - [Fact] - public void TestTypedParamsWithNoParams() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(p => p.Submit(It.IsAny(), It.IsAny())); - - mock.Object.Submit("foo"); - - mock.VerifyAll(); - } - - [Fact] - public void TestTypedParams() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(p => p.Submit(It.IsAny(), It.IsAny())); - - mock.Object.Submit("foo", 0, 1, 2); - - mock.VerifyAll(); - } - - [Fact] - public void TestObjectParamsWithoutParams() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(p => p.Execute(It.IsAny(), It.IsAny())); - - mock.Object.Execute(1); - - mock.VerifyAll(); - } - - [Fact] - public void TestObjectParams() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(p => p.Execute(It.IsAny(), It.IsAny())); - - mock.Object.Execute(1, "0", "1", "2"); - - mock.VerifyAll(); - } - - [Fact] - public void TestObjectParamsWithExpectedValues() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(p => p.Execute(5, "foo", "bar")); - - Assert.Throws(() => mock.Object.Execute(5, "bar", "foo")); - - mock.Object.Execute(5, "foo", "bar"); - - mock.Verify(p => p.Execute(5, "foo", "bar")); - } - - [Fact] - public void TestObjectParamsWithArray() - { - var mock = new Mock(); - mock.Setup(p => p.Execute(It.IsAny(), It.IsAny(), It.IsAny())); - - mock.Object.Execute(1, new string[] { "0", "1" }, 3); - - mock.Verify(p => p.Execute(It.IsAny(), It.IsAny())); - mock.Verify(p => p.Execute(It.IsAny(), It.IsAny(), It.IsAny())); - mock.Verify(p => p.Execute(It.IsAny(), It.IsAny(), It.IsAny())); - } - - [Fact] - public void TestTypedParamsInEachArgument() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(p => p.Submit(It.IsAny(), It.IsAny(), It.IsAny())); - - mock.Object.Submit("foo", 0, 1); - - mock.Verify(p => p.Submit(It.IsAny(), It.IsAny(), It.IsAny())); - mock.Verify(p => p.Submit(It.IsAny(), It.Is(a => a.Length == 2))); - mock.VerifyAll(); - } - - [Fact] - public void TestParamsWithReturnValue() - { - var mock = new Mock(); - mock.Setup(x => x.GetValue("Matt")).Returns("SomeString"); - - var ret = mock.Object.GetValue("Matt"); - Assert.Equal("SomeString", ret); - } - - public interface IParams - { - void Submit(string name, params int[] values); - void Execute(int value, params object[] values); - string GetValue(string name, params object[] args); - } - } - - #endregion - - #region #159 - - public class _159 - { - [Fact] - public void ImplicitIntToLong() - { - int id = 1; - var mock = new Mock(); - mock.Object.SetIt(id); - mock.Verify(x => x.SetIt(id)); - } - - [Fact] - public void ImplicitInterface() - { - var barMock = new Mock(); - var baz = new Baz(barMock.Object); - baz.DoBarFoo(new Foo()); - barMock.Verify(x => x.DoFoo(It.IsAny())); - } - - public interface IFoo - { - long Id { get; set; } - void SetIt(long it); - } - - public class Foo : IFoo - { - public long Id { get; set; } - public void SetIt(long it) { } - } - - public interface IBar - { - void DoFoo(IFoo foo); - } - - public class Baz - { - private readonly IBar _bar; - public Baz(IBar bar) - { - _bar = bar; - } - - public void DoBarFoo(IFoo foo) - { - _bar.DoFoo(foo); - } - } - } - - #endregion - - #region #152 - - public class _152 - { - public enum MembershipCreateStatus { Created, Duplicated, Invalid } - public interface IMembershipService - { - int MinPasswordLength { get; } - bool ValidateUser(string userName, string password); - MembershipCreateStatus CreateUser(string userName, string password, string email); - bool ChangePassword(string userName, string oldPassword, string newPassword); - } - - [Fact] - public void ShouldReturnEnum() - { - var provider = new Mock(); - - // For some reason, this particular lambda doesn't let me specify - // a method return value for the method even though it returns a - // MembershipCreateStatus enum - provider.Setup(p => p.CreateUser(string.Empty, string.Empty, string.Empty)).Returns(MembershipCreateStatus.Invalid); - - Assert.Equal(MembershipCreateStatus.Invalid, provider.Object.CreateUser("", "", "")); - } - } - - #endregion - - #region #153 - - public class _153 - { - public struct SomeClass // Struct just to avoid having to implement Equals/GetHashCode - { - public static implicit operator SomeClass(T t) - { - return new SomeClass(); - } - - public static SomeClass From(T t) - { - return t; - } - } - - public interface IIfc - { - int Get(SomeClass id); - } - - public class ImplicitConversionProblem - { - [Fact] - public void ImplicitSetupVerifyAll_Fails() - { - const string s = "XYZ"; - var mock = new Mock(); - mock.Setup(ifc => ifc.Get(s)).Returns(17); - - var result = mock.Object.Get(s); - - mock.VerifyAll(); // MockVerificationException here - Assert.Equal(17, result); - } - - [Fact] - public void ExplicitSetupVerifyAll_Works() - { - const string s = "XYZ"; - var mock = new Mock(); - mock.Setup(ifc => ifc.Get(SomeClass.From(s))).Returns(17); - - var result = mock.Object.Get(s); - - mock.VerifyAll(); - Assert.Equal(17, result); - } - - [Fact] - public void ExplicitSetupImplicitVerification_Fails() - { - const string s = "XYZ"; - var mock = new Mock(); - mock.Setup(ifc => ifc.Get(SomeClass.From(s))).Returns(17); - - var result = mock.Object.Get(s); - - // Here the problem can be seen even in the exception message: - // Invocation was not performed on the mock: ifc => ifc.Get("XYZ") - // -----------------------------------------------------------^ - mock.Verify(ifc => ifc.Get(s)); - Assert.Equal(17, result); - } - - [Fact] - public void ImplicitSetupExplicitVerification_Fails() - { - const string s = "XYZ"; - var mock = new Mock(); - mock.Setup(ifc => ifc.Get(s)).Returns(17); - - var result = mock.Object.Get(s); - - // This verification passes oddly enough - mock.Verify(ifc => ifc.Get(SomeClass.From(s))); - - // This assert fails, indicating that the setup was not used - Assert.Equal(17, result); - } - } - } - - #endregion - - #region #146 - - public class _146 - { - public interface IFoo - { - bool Property { get; set; } - string StringProperty { get; set; } - } - - [Fact] - public void StrictMockPropertySet() - { - var mock = new Mock(MockBehavior.Strict); - - mock.SetupSet(v => v.Property = false); - - Assert.Throws(() => mock.VerifySet(v => v.Property = false)); - - mock.Object.Property = false; - - mock.VerifySet(v => v.Property = false); - } - } - - #endregion - - #region #158 - - public class _158 - { - public class Foo - { - public virtual void Boo() - { - Bar(); - Bar(); - } - - protected virtual void Bar() - { - } - } - -#pragma warning disable 618 - [Fact(Skip = "This setup doesn't make sense, and xUnit does not provide this message checking capability.")] - public void ShouldRenderCustomMessage() - { - var foo = new Mock { CallBase = true }; - foo.Protected().Setup("Bar").AtMostOnce().Verifiable("Hello"); - foo.Object.Boo(); - //Assert.Throws("Hello", () => foo.Object.Boo()); - } -#pragma warning restore 618 - } - - #endregion - - #region #160 - -#if !SILVERLIGHT - public class _160 - { - [Fact] - public void ShouldMockHtmlControl() - { - // CallBase was missing - var htmlInputTextMock = new Mock() { CallBase = true }; - Assert.True(htmlInputTextMock.Object.Visible); - } - } -#endif - - #endregion - - #region #161 - - public class _161 - { - [Fact] - public void InvertEqualObjects() - { - var foo1 = new Foo { Id = "1" }; - var foo = new Foo { Id = "2" }; - - var dependency = new Mock(); - - dependency.Setup(x => x.DoThis(foo, foo1)) - .Returns(new Foo()); - - var f = dependency.Object.DoThis(foo, foo1); - - dependency.Verify(x => x.DoThis(foo, foo1)); - dependency.Verify(x => x.DoThis(foo1, foo), Times.Never()); - } - - [Fact(Skip = "Wrong Equals implemention in the report. Won't Fix")] - public void ExampleFailingTest() - { - var foo1 = new Foo(); - var foo = new Foo(); - - var sut = new Perfectly_fine_yet_failing_test(); - var dependency = new Mock(); - - dependency.Setup(x => x.DoThis(foo, foo1)) - .Returns(new Foo()); - - sut.Do(dependency.Object, foo, foo1); - - dependency.Verify(x => x.DoThis(foo, foo1)); - dependency.Verify(x => x.DoThis(foo1, foo), Times.Never()); - } - - public class Perfectly_fine_yet_failing_test - { - public void Do(IDependency dependency, Foo foo, Foo foo1) - { - var foo2 = dependency.DoThis(foo, foo1); - if (foo2 == null) - foo2 = dependency.DoThis(foo1, foo); - } - } - - public interface IDependency - { - Foo DoThis(Foo foo, Foo foo1); - } - - public class Foo - { - public string Id { get; set; } - - public override bool Equals(object obj) - { - return obj is Foo && ((Foo)obj).Id == Id; - } - - public override int GetHashCode() - { - return base.GetHashCode(); - } - } - } - - #endregion - - #region #174 - - public class _174 - { - [Fact] - public void Test() - { - var serviceNo1Mock = new Mock(); - var collaboratorMock = new Mock(); - - collaboratorMock.Object.Collaborate(serviceNo1Mock.Object); - - collaboratorMock.Verify(o => o.Collaborate(serviceNo1Mock.Object)); - } - - public interface ISomeCollaborator - { - void Collaborate(IServiceNo1 serviceNo1); - } - - public interface IServiceNo1 : IEnumerable - { - } - } - - #endregion - - #region #177 - - public class _177 - { - [Fact] - public void Test() - { - var mock = new Mock(); - Assert.NotNull(mock.Object); - } - - public interface IMyInterface - { - void DoStuff() where TTo : TFrom; - } - } - - #endregion - - #region #184 - - public class _184 - { - [Fact] - public void Test() - { - var fooRaised = false; - var barRaised = false; - - var fooMock = new Mock(); - var barMock = fooMock.As(); - - fooMock.Object.FooEvent += (s, e) => fooRaised = true; - barMock.Object.BarEvent += (s, e) => barRaised = true; - - fooMock.Raise(m => m.FooEvent += null, EventArgs.Empty); - barMock.Raise(m => m.BarEvent += null, EventArgs.Empty); - - Assert.True(fooRaised); - Assert.True(barRaised); - } - - public interface IFoo - { - event EventHandler FooEvent; - } - - public interface IBar - { - event EventHandler BarEvent; - } - } - - #endregion - - #region #185 - - public class _185 - { - [Fact] - public void Test() - { - var mock = new Mock>(); - Assert.Throws( - Resources.SetupOnNonMemberMethod, - () => mock.Setup(l => l.FirstOrDefault()).Returns("Hello world")); - } - } - - #endregion - - #region #187 - - public class _187 - { - [Fact] - public void Test() - { - var mock = new Mock(); - - mock.Setup(r => r.Get()).Returns(new Object()); - mock.Setup(r => r.Get()).Returns(new Object()); - - Assert.NotNull(mock.Object.Get()); - Assert.NotNull(mock.Object.Get()); - } - - public class Foo - { - public class Inner - { - } - } - - public class Bar - { - public class Inner - { - } - } - - public interface IGeneric - { - object Get() where T : new(); - } - } - - #endregion - - #region #183 - - public class _183 - { - [Fact] - public void Test() - { - var mock = new Mock(); - mock.Setup(m => m.Execute(1)); - mock.Setup(m => m.Execute(It.IsInRange(2, 20, Range.Exclusive))); - mock.Setup(m => m.Execute(3, "Caption")); - - mock.Object.Execute(3); - mock.Object.Execute(4); - mock.Object.Execute(5); - - var e = Assert.Throws(() => mock.Verify(m => m.Execute(0))); - Assert.Contains( - "\r\nConfigured setups:" + - "\r\nm => m.Execute(1), Times.Never" + - "\r\nm => m.Execute(It.IsInRange(2, 20, Range.Exclusive)), Times.Exactly(3)", - e.Message); - } - - [Fact] - public void TestGeneric() - { - var mock = new Mock(); - mock.Setup(m => m.Execute(1, 10)); - mock.Setup(m => m.Execute(1, "Foo")); - - mock.Object.Execute(1, 10); - - var e = Assert.Throws(() => mock.Verify(m => m.Execute(1, 1))); - Assert.Contains( - "\r\nConfigured setups:\r\nm => m.Execute(1, 10), Times.Once", - e.Message); - } - - [Fact] - public void TestNoSetups() - { - var mock = new Mock(); - - var e = Assert.Throws(() => mock.Verify(m => m.Execute(1))); - Assert.Contains("\r\nNo setups configured.", e.Message); - - } - - public interface IFoo - { - void Execute(int param); - void Execute(int param, string caption); - void Execute(int p, T param); - } - } - - #endregion - - #region #186 - - public class _186 - { - [Fact] - public void TestVerifyMessage() - { - var mock = new Mock(); - mock.Setup(m => m.OnExecute()); - - var e = Assert.Throws(() => mock.Verify(m => m.Execute())); - Assert.True(e.Message.StartsWith("Invalid verify")); - } - - public class Foo - { - public void Execute() - { - this.OnExecute(); - } - - public virtual void OnExecute() - { - throw new NotImplementedException(); - } - } - } - - #endregion - - #region #190 - - public class _190 - { - [Fact] - public void Test() - { - var mock = new Mock().As(); - mock.SetupAllProperties(); - - ISite site = new FooSite(); - mock.Object.Site = site; - Assert.Same(site, mock.Object.Site); - } - - public class FooSite : ISite - { - public IComponent Component - { - get { throw new NotImplementedException(); } - } - - public IContainer Container - { - get { throw new NotImplementedException(); } - } - - public bool DesignMode - { - get { throw new NotImplementedException(); } - } - - public string Name - { - get { throw new NotImplementedException(); } - set { throw new NotImplementedException(); } - } - - public object GetService(Type serviceType) - { - throw new NotImplementedException(); - } - } - - } - - #endregion - - #region #204 - - public class _204 - { - [Fact] - public void Test() - { - var mock = new Mock(); - mock.Setup(x => x.Select(u => u.Id == 100)) - .Returns(new User() { Id = 100 }); - - var user = mock.Object.Select(usr => usr.Id == 100); - Assert.Equal(100, user.Id); - mock.Verify(x => x.Select(usr => usr.Id == 100), Times.Once()); - - user = mock.Object.Select(usr => usr.Id == 101); - Assert.Null(user); - mock.Verify(x => x.Select(usr => usr.Id == 101), Times.Once()); - - mock.Verify(x => x.Select(usr => usr.Id == 102), Times.Never()); - mock.Verify(x => x.Select(It.IsAny>>()), Times.Exactly(2)); - } - - public interface IRepository - { - T Select(Expression> filter) where T : class; - } - - public class User - { - public int Id { get; set; } - } - } - - #endregion - - #region #205 - - public class _205 - { - [Fact] - public void Test() - { - Assert.DoesNotThrow(() => new Mock().SetupAllProperties()); - } - - public interface IFoo - { - string Error { get; set; } - string this[int index] { get; set; } - } - } - - #endregion - - #region #223 - - public class _223 - { - [Fact] - public void TestSetup() - { - this.TestSetupHelper(); - } - - public void TestSetupHelper() where T : class, IFoo - { - var expected = 2; - - var target = new Mock(); - target.Setup(p => p.DoInt32(0)).Returns(expected); - target.Setup(p => p.DoGeneric(0)).Returns(expected); - - Assert.Equal(expected, target.Object.DoInt32(0)); - Assert.Equal(expected, target.Object.DoGeneric(0)); - } - - public interface IFoo - { - int DoInt32(int value); - T DoGeneric(int value); - } - - public class Foo : IFoo - { - public virtual int DoInt32(int value) - { - return 4; - } - - public virtual int DoGeneric(int value) - { - return 5; - } - } - } - - #endregion - - #region #229 - - public class _229 - { - [Fact] - public void Test() - { - var target = new Mock { CallBase = true }; - - var raised = false; - target.Object.MyEvent += (s, e) => raised = true; - target.Object.RaiseMyEvent(); - - Assert.True(raised); - } - - public class Foo - { - public virtual event EventHandler MyEvent; - - public void RaiseMyEvent() - { - if (this.MyEvent != null) - { - this.MyEvent(this, EventArgs.Empty); - } - } - } - } - - #endregion - - #region #230 - - public class _230 - { - [Fact] - public void ByteArrayCallbackArgumentShouldNotBeNull() - { - var data = new byte[] { 2, 1, 2 }; - var stream = new Mock(); - - stream.SetupGet(m => m.Length) - .Returns(data.Length); - stream.Setup(m => m.Read(It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((b, o, c) => data.CopyTo(b, 0)) - .Returns(data.Length); - - var contents = new byte[stream.Object.Length]; - Assert.DoesNotThrow(() => stream.Object.Read(contents, 0, (int)stream.Object.Length)); - } - } - - #endregion - - #region #232 - - public class _232 - { - [Fact] - public void Test() - { - var repository = new Mock(); - var svc = new Service(repository.Object); - - svc.Create(); - - repository.Verify(r => r.Insert(It.IsAny()), Times.Once()); - repository.Verify(r => r.Insert(It.IsAny()), Times.Once()); - repository.Verify(r => r.Insert(It.IsAny()), Times.Exactly(2)); - } - - public interface IRepository - { - void Insert(IEntity entity); - } - - public interface IEntity - { - } - - public class Foo : IEntity - { - } - - public class Bar : IEntity - { - } - - public class Service - { - private IRepository repository; - - public Service(IRepository repository) - { - this.repository = repository; - } - - public void Create() - { - repository.Insert(new Foo()); - repository.Insert(new Bar()); - } - } - } - - #endregion - - #region #242 - - public class _242 - { - [Fact] - public void PropertyChangedTest() - { - var mock = new Mock(); - int callbacks = 0; - mock.Object.PropertyChanged += (sender, args) => callbacks++; - - mock.Raise(m => m.PropertyChanged += null, new PropertyChangedEventArgs("Foo")); - Assert.Equal(1, callbacks); - } - - public class PropertyChangedBase : INotifyPropertyChanged - { - public virtual event PropertyChangedEventHandler PropertyChanged = (s, e) => { }; - } - - public class PropertyChangedInherited : PropertyChangedBase - { - } - } - - #endregion - - #region #245 - - public class _245 - { - [Fact] - public void Test() - { - var mock = new Mock(); - - ITest instance; - Assert.DoesNotThrow(() => instance = mock.Object); - } - - public interface ITest - { - void Do() where T2 : T1; - } - } - - #endregion - - #region #251 - - public class _251 - { - [Fact] - public void Test() - { - var repositoryMock = new Mock>(); - - var repository = repositoryMock.Object; - repository.Save("test"); - - repositoryMock.Verify(m => m.Save("test")); - } - - public interface IRepository - { - void Save(string value); - } - - public interface IRepository : IRepository - { - void Save(T value); - } - } - - #endregion - - #region #256 - - public class _256 - { - [Fact] - public void TestFinalizeNotMocked() - { - var mock = new Mock(MockBehavior.Strict); - mock.Setup(m => m.Foo).Returns(10); - mock.Setup(m => m.Bar).Returns("Hello mocked world!"); - var instance = mock.Object; - - Assert.Equal(10, instance.Foo); - } - - public class ClassWithFinalizer - { - public virtual int Foo { get; set; } - public virtual string Bar { get; set; } - - ~ClassWithFinalizer() - { - - } - } - } - - #endregion - - #region #261 - - public class _261 - { - [Fact] - public void Test() - { - var mock = new Mock(); - mock.Protected().SetupSet("Status", 42); - - mock.Object.SetStatus(42); - - mock.Protected().VerifySet("Status", Times.Once(), 42); - } - - public class Foo - { - public virtual int Status { get; protected set; } - - internal void SetStatus(int value) - { - this.Status = value; - } - } - } - - #endregion - - #region #267 - - public class _267 - { - public interface IPerformOperation - { - string Operation(object input); - } - - public class OperationUser - { - private readonly IPerformOperation m_OperationPerformer; - - public OperationUser(IPerformOperation operationPerformer) - { - m_OperationPerformer = operationPerformer; - } - - public string DoOperation(object input) - { - return m_OperationPerformer.Operation(input); - } - } - - public class HelperSetup - { - private Mock m_OperationStub; - - public HelperSetup() - { - m_OperationStub = new Mock(); - } - - [Fact] - public void InlineSetupTest() - { - m_OperationStub.Setup(m => m.Operation(It.IsAny())).Returns(value => "test"); - m_OperationStub.Setup(m => m.Operation(It.IsAny())).Returns(value => "25"); - - var operationUser = new OperationUser(m_OperationStub.Object); - - var intOperationResult = operationUser.DoOperation(9); - var stringOperationResult = operationUser.DoOperation("Hello"); - - Assert.Equal("25", intOperationResult); - Assert.Equal("test", stringOperationResult); - } - - [Fact] - public void HelperSetupTest() - { - SetupOperationStub(value => "test"); - SetupOperationStub(value => "25"); - - var operationUser = new OperationUser(m_OperationStub.Object); - - var intOperationResult = operationUser.DoOperation(9); - var stringOperationResult = operationUser.DoOperation("Hello"); - - Assert.Equal("25", intOperationResult); - Assert.Equal("test", stringOperationResult); - } - - private void SetupOperationStub(Func valueFunction) - { - m_OperationStub.Setup(m => m.Operation(It.IsAny())).Returns(valueFunction); - } - } - } - - #endregion - - #region #273 - -#if !SILVERLIGHT - - public class _273 - { - [Fact] - public void WhenMockingAnExternalInterface_ThenItWorks() - { - Assert.NotNull(new Mock().Object); - Assert.NotNull(Mock.Of()); - Assert.NotNull(new Mock().Object); - Assert.NotNull(new Mock().Object); - Assert.NotNull(Mock.Of()); - Assert.NotNull(new Mock().Object); - Assert.NotNull(new Mock().Object); - } - - public class Baz : ClassLibrary2.Bar - { - } - } - -#endif - - #endregion - - #region #325 - - public class _325 - { - [Fact] - public void SubscribingWorks() - { - var target = new Mock { CallBase = true }; - target.As(); - - var bar = (IBar)target.Object; - var raised = false; - bar.SomeEvent += (sender, e) => raised = true; - - target.As().Raise(b => b.SomeEvent += null, EventArgs.Empty); - - Assert.True(raised); - } - - [Fact] - public void UnsubscribingWorks() - { - var target = new Mock { CallBase = true }; - target.As(); - - var bar = (IBar)target.Object; - var raised = false; - EventHandler handler = (sender, e) => raised = true; - bar.SomeEvent += handler; - bar.SomeEvent -= handler; - - target.As().Raise(b => b.SomeEvent += null, EventArgs.Empty); - - Assert.False(raised); - } - - public class Foo - { - } - - public interface IBar - { - event EventHandler SomeEvent; - } - - } - - #endregion - - #region Recursive issue - - public class RecursiveFixture - { - [Fact] - public void TestRecursive() - { - var mock = new Mock() { DefaultValue = DefaultValue.Mock }; - mock.Setup(c => c.HttpContext.Response.Write("stuff")); - - mock.Object.HttpContext.Response.Write("stuff"); - mock.Object.HttpContext.Response.ShouldEncode = true; - - Assert.Throws(() => mock.VerifySet( - c => c.HttpContext.Response.ShouldEncode = It.IsAny(), - Times.Never())); - } - - public class ControllerContext - { - public virtual HttpContext HttpContext { get; set; } - } - - public abstract class HttpContext - { - protected HttpContext() - { - } - - public virtual HttpResponse Response - { - get { throw new NotImplementedException(); } - } - } - - public abstract class HttpResponse - { - protected HttpResponse() - { - } - - public virtual bool ShouldEncode - { - get { throw new NotImplementedException(); } - set { throw new NotImplementedException(); } - } - - public virtual void Write(string s) - { - throw new NotImplementedException(); - } - } - } - - #endregion - - #region Silverlight excluded - -#if !SILVERLIGHT - - #region #250 - - /// - /// Silverlight MethodInfo protected constructor is internal, unlike desktop .NET - /// - public class _250 - { - [Fact] - public void Test() - { - var target = new Mock(); - - Assert.NotNull(target.Object); - } - } - - #endregion - - #region #250 - - public class _254 - { - [Fact] - public void ShouldMockInteropDTE() - { - var dte = new Mock(); - - Assert.NotNull(dte.Object); - } - -#if !NET3x && !SERVER - [Fact] - public void ShouldRaiseEventOnInteropInterface() - { - var app = new Mock(); - var count = 0; - app.Object.DocumentOpen += doc => count++; - - app.Raise(x => x.DocumentOpen += null, new Mock().Object); - - Assert.Equal(1, count); - } -#endif - } - - #endregion - - // run "netsh http add urlacl url=http://+:7777/ user=[domain]\[user]" - // to avoid running the test as an admin - [Fact(Skip = "Doesn't work in Mono")] - public void ProxiesAndHostsWCF() - { -#if DEBUG - // On release mode, castle is ILMerged into Moq.dll and this won't compile - var generator = new Castle.DynamicProxy.ProxyGenerator(); - var proxy = generator.CreateClassProxy(); - using (var host = new WebServiceHost(proxy, new Uri("http://localhost:7777"))) - { - host.Open(); - } -#endif - } - - // run "netsh http add urlacl url=http://+:7777/ user=[domain]\[user]" - // to avoid running the test as an admin - [Fact(Skip = "Doesn't work in Mono")] - public void ProxiesAndHostsWCFMock() - { - //var generator = new Castle.DynamicProxy.ProxyGenerator(); - var proxy = new Mock(); - using (var host = new WebServiceHost(proxy.Object, new Uri("http://localhost:7777"))) - { - host.Open(); - } - } - - [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] - public class ServiceImplementation : IServiceContract - { - public void Do() - { - throw new NotImplementedException(); - } - } - - [ServiceContract] - public interface IServiceContract - { - [OperationContract] - void Do(); - } - -#endif - - #endregion - } +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; +using Moq; +using Moq.Properties; +using Moq.Protected; +using Xunit; + +#if !SILVERLIGHT +using System.ServiceModel; +using System.ServiceModel.Web; +using System.Web.UI.HtmlControls; +#endif + +#region #181 + +// NOTE class without namespace +public class _181 +{ + [Fact] + public void ReproTest() + { + var mock = new Mock(); + mock.Object.Dispose(); + + mock.Verify(d => d.Dispose()); + } +} + +#endregion + +namespace Moq.Tests.Regressions +{ + public class IssueReportsFixture + { + #region #47 + + [Fact] + public void ShouldReturnListFromDateTimeArg() + { + var items = new List() { "Foo", "Bar" }; + + var mock = new Mock(MockBehavior.Strict); + mock + .Setup(m => m.GetValuesSince(It.IsAny())) + .Returns(items); + + var actual = mock.Object.GetValuesSince(DateTime.Now).ToList(); + + Assert.Equal(items.Count, actual.Count); + } + + public interface IMyClass + { + IEnumerable GetValuesSince(DateTime since); + } + + #endregion + + #region #48 + + public class Issue48 + { + [Fact] + public void ExpectsOnIndexer() + { + var mock = new Mock(); + mock.Setup(m => m[0]).Returns("a"); + mock.Setup(m => m[1]).Returns("b"); + + Assert.Equal("a", mock.Object[0]); + Assert.Equal("b", mock.Object[1]); + Assert.Equal(default(string), mock.Object[2]); + } + + public interface ISomeInterface + { + string this[int index] { get; set; } + } + } + + #endregion + + #region #52 + + [Fact] + public void ShouldNotOverridePreviousExpectation() + { + var ids = Enumerable.Range(1, 10); + var mock = new Mock(MockBehavior.Strict); + + foreach (var id in ids) + { + mock.Setup(x => x.DoSomething(id)); + } + + var component = mock.Object; + + foreach (var id in ids) + { + component.DoSomething(id); + } + } + + public interface IOverwritingMethod + { + void DoSomething(int id); + } + + #endregion + + #region #62 + + public interface ISomething + { + void DoSomething() where U : T; + } + + [Fact] + public void CreatesMockWithGenericsConstraints() + { + var mock = new Mock>(); + } + + #endregion + + #region #60 + + public interface IFoo + { + void DoThings(object arg); + } + + [Fact] + public void TwoExpectations() + { + Mock mocked = new Mock(MockBehavior.Strict); + object arg1 = new object(); + object arg2 = new object(); + + mocked.Setup(m => m.DoThings(arg1)); + mocked.Setup(m => m.DoThings(arg2)); + + mocked.Object.DoThings(arg1); + mocked.Object.DoThings(arg2); + + mocked.VerifyAll(); + } + + #endregion + + #region #21 + + [Fact] + public void MatchesLatestExpectations() + { + var mock = new Mock(); + + mock.Setup(m => m.Method(It.IsAny())).Returns(0); + mock.Setup(m => m.Method(It.IsInRange(0, 20, Range.Inclusive))).Returns(1); + + mock.Setup(m => m.Method(5)).Returns(2); + mock.Setup(m => m.Method(10)).Returns(3); + + Assert.Equal(3, mock.Object.Method(10)); + Assert.Equal(2, mock.Object.Method(5)); + Assert.Equal(1, mock.Object.Method(6)); + Assert.Equal(0, mock.Object.Method(25)); + } + + public interface IEvaluateLatest + { + int Method(int value); + } + + #endregion + + #region #49 + + [Fact] + public void UsesCustomMatchersWithGenerics() + { + var mock = new Mock(); + + mock.Setup(e => e.Method(IsEqual.To(5))).Returns(1); + mock.Setup(e => e.Method(IsEqual.To(6, "foo"))).Returns(2); + + Assert.Equal(1, mock.Object.Method(5)); + Assert.Equal(2, mock.Object.Method(6)); + } + + public static class IsEqual + { +#pragma warning disable 618 + [Matcher] + public static T To(T value) + { + return value; + } +#pragma warning restore 618 + + public static bool To(T left, T right) + { + return left.Equals(right); + } + +#pragma warning disable 618 + [Matcher] + public static T To(T value, U value2) + { + return value; + } +#pragma warning restore 618 + + public static bool To(T left, T right, U value) + { + return left.Equals(right); + } + } + + #endregion + + #region #68 + + [Fact] + public void GetMockCastedToObjectThrows() + { + var mock = new Mock(); + object m = mock.Object; + + Assert.Throws(() => Mock.Get(m)); + } + + #endregion + + #region #69 + + public interface IFooPtr + { + IntPtr Get(string input); + } + + [Fact] + public void ReturnsIntPtr() + { + Mock mock = new Mock(MockBehavior.Strict); + IntPtr ret = new IntPtr(3); + + mock.Setup(m => m.Get("a")).Returns(ret); + + IntPtr ret3 = mock.Object.Get("a"); + + Assert.Equal(ret, mock.Object.Get("a")); + } + + + #endregion + + #region #85 + + public class Issue85 + { + [Fact] + public void FooTest() + { + // Setup + var fooMock = new Mock(); + fooMock.CallBase = true; + fooMock.Setup(o => o.GetBar()).Returns(new Bar()); + var bar = ((IFoolery)fooMock.Object).DoStuffToBar(); + Assert.NotNull(bar); + } + + public interface IFoolery + { + Bar DoStuffToBar(); + } + + public class Foo : IFoolery + { + public virtual Bar GetBar() + { + return new Bar(); + } + + Bar IFoolery.DoStuffToBar() + { + return DoWeirdStuffToBar(); + } + + protected internal virtual Bar DoWeirdStuffToBar() + { + var bar = GetBar(); + //Would do stuff here. + return bar; + } + } + + public class Bar + { + } + } + + #endregion + + #region #89 + + public class Issue89 + { + [Fact] + public void That_last_expectation_should_win() + { + var mock = new Mock(); + mock.Setup(s => s.Get(1)).Returns("blah"); + mock.Setup(s => s.Get(It.IsAny())).Returns("foo"); + mock.Setup(s => s.Get(1)).Returns("bar"); + Assert.Equal("bar", mock.Object.Get(1)); + } + + public interface ISample + { + string Get(int i); + } + } + + #endregion + + #region #134 + + public class Issue134 + { + [Fact] + public void Test() + { + var target = new Mock(); + target.Setup(t => t.Submit(It.IsAny(), It.IsAny(), It.IsAny())); + + var e = Assert.Throws(() => target.VerifyAll()); + + Assert.Contains( + "IFoo t => t.Submit(It.IsAny(), It.IsAny(), new[] { It.IsAny() })", + e.Message); + } + + public interface IFoo + { + void Submit(string mailServer, string from, params int[] toRecipient); + } + } + + #endregion + + #region #136 + + public class _136 + { + // Fixed on PropertiesFixture.cs + } + + #endregion + + #region #138 + + public class _138 + { + public interface SuperFoo + { + string Bar { get; set; } + } + public interface Foo : SuperFoo + { + string Baz { get; set; } + } + + [Fact] + public void superFooMockSetupAllProperties() + { + var repo = new MockRepository(MockBehavior.Default); + var superFooMock = repo.Create(); + superFooMock.SetupAllProperties(); + + var superFoo = superFooMock.Object; + superFoo.Bar = "Bar"; + Assert.Equal("Bar", superFoo.Bar); + } + } + + #endregion + + #region #145 + + public class _145 + { + public interface IResolver + { + string Resolve(); + } + + public class DataWriter + { + } + + public class DataA { } + public class DataB { } + + [Fact] + public void ShouldDifferentiateBetweenGenericsParams() + { + var mock = new Mock(); + mock.Setup(m => m.Resolve>()).Returns("Success A"); + + Assert.Equal("Success A", mock.Object.Resolve>()); + + mock.Setup(m => m.Resolve>()).Returns("Success B"); + + Assert.Equal("Success B", mock.Object.Resolve>()); + Assert.Equal("Success A", mock.Object.Resolve>()); + } + + } + + #endregion + + #region #111 & #155 + + public class _111 + { + [Fact] + public void TestTypedParamsWithNoParams() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(p => p.Submit(It.IsAny(), It.IsAny())); + + mock.Object.Submit("foo"); + + mock.VerifyAll(); + } + + [Fact] + public void TestTypedParams() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(p => p.Submit(It.IsAny(), It.IsAny())); + + mock.Object.Submit("foo", 0, 1, 2); + + mock.VerifyAll(); + } + + [Fact] + public void TestObjectParamsWithoutParams() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(p => p.Execute(It.IsAny(), It.IsAny())); + + mock.Object.Execute(1); + + mock.VerifyAll(); + } + + [Fact] + public void TestObjectParams() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(p => p.Execute(It.IsAny(), It.IsAny())); + + mock.Object.Execute(1, "0", "1", "2"); + + mock.VerifyAll(); + } + + [Fact] + public void TestObjectParamsWithExpectedValues() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(p => p.Execute(5, "foo", "bar")); + + Assert.Throws(() => mock.Object.Execute(5, "bar", "foo")); + + mock.Object.Execute(5, "foo", "bar"); + + mock.Verify(p => p.Execute(5, "foo", "bar")); + } + + [Fact] + public void TestObjectParamsWithArray() + { + var mock = new Mock(); + mock.Setup(p => p.Execute(It.IsAny(), It.IsAny(), It.IsAny())); + + mock.Object.Execute(1, new string[] { "0", "1" }, 3); + + mock.Verify(p => p.Execute(It.IsAny(), It.IsAny())); + mock.Verify(p => p.Execute(It.IsAny(), It.IsAny(), It.IsAny())); + mock.Verify(p => p.Execute(It.IsAny(), It.IsAny(), It.IsAny())); + } + + [Fact] + public void TestTypedParamsInEachArgument() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(p => p.Submit(It.IsAny(), It.IsAny(), It.IsAny())); + + mock.Object.Submit("foo", 0, 1); + + mock.Verify(p => p.Submit(It.IsAny(), It.IsAny(), It.IsAny())); + mock.Verify(p => p.Submit(It.IsAny(), It.Is(a => a.Length == 2))); + mock.VerifyAll(); + } + + [Fact] + public void TestParamsWithReturnValue() + { + var mock = new Mock(); + mock.Setup(x => x.GetValue("Matt")).Returns("SomeString"); + + var ret = mock.Object.GetValue("Matt"); + Assert.Equal("SomeString", ret); + } + + public interface IParams + { + void Submit(string name, params int[] values); + void Execute(int value, params object[] values); + string GetValue(string name, params object[] args); + } + } + + #endregion + + #region #159 + + public class _159 + { + [Fact] + public void ImplicitIntToLong() + { + int id = 1; + var mock = new Mock(); + mock.Object.SetIt(id); + mock.Verify(x => x.SetIt(id)); + } + + [Fact] + public void ImplicitInterface() + { + var barMock = new Mock(); + var baz = new Baz(barMock.Object); + baz.DoBarFoo(new Foo()); + barMock.Verify(x => x.DoFoo(It.IsAny())); + } + + public interface IFoo + { + long Id { get; set; } + void SetIt(long it); + } + + public class Foo : IFoo + { + public long Id { get; set; } + public void SetIt(long it) { } + } + + public interface IBar + { + void DoFoo(IFoo foo); + } + + public class Baz + { + private readonly IBar _bar; + public Baz(IBar bar) + { + _bar = bar; + } + + public void DoBarFoo(IFoo foo) + { + _bar.DoFoo(foo); + } + } + } + + #endregion + + #region #152 + + public class _152 + { + public enum MembershipCreateStatus { Created, Duplicated, Invalid } + public interface IMembershipService + { + int MinPasswordLength { get; } + bool ValidateUser(string userName, string password); + MembershipCreateStatus CreateUser(string userName, string password, string email); + bool ChangePassword(string userName, string oldPassword, string newPassword); + } + + [Fact] + public void ShouldReturnEnum() + { + var provider = new Mock(); + + // For some reason, this particular lambda doesn't let me specify + // a method return value for the method even though it returns a + // MembershipCreateStatus enum + provider.Setup(p => p.CreateUser(string.Empty, string.Empty, string.Empty)).Returns(MembershipCreateStatus.Invalid); + + Assert.Equal(MembershipCreateStatus.Invalid, provider.Object.CreateUser("", "", "")); + } + } + + #endregion + + #region #153 + + public class _153 + { + public struct SomeClass // Struct just to avoid having to implement Equals/GetHashCode + { + public static implicit operator SomeClass(T t) + { + return new SomeClass(); + } + + public static SomeClass From(T t) + { + return t; + } + } + + public interface IIfc + { + int Get(SomeClass id); + } + + public class ImplicitConversionProblem + { + [Fact] + public void ImplicitSetupVerifyAll_Fails() + { + const string s = "XYZ"; + var mock = new Mock(); + mock.Setup(ifc => ifc.Get(s)).Returns(17); + + var result = mock.Object.Get(s); + + mock.VerifyAll(); // MockVerificationException here + Assert.Equal(17, result); + } + + [Fact] + public void ExplicitSetupVerifyAll_Works() + { + const string s = "XYZ"; + var mock = new Mock(); + mock.Setup(ifc => ifc.Get(SomeClass.From(s))).Returns(17); + + var result = mock.Object.Get(s); + + mock.VerifyAll(); + Assert.Equal(17, result); + } + + [Fact] + public void ExplicitSetupImplicitVerification_Fails() + { + const string s = "XYZ"; + var mock = new Mock(); + mock.Setup(ifc => ifc.Get(SomeClass.From(s))).Returns(17); + + var result = mock.Object.Get(s); + + // Here the problem can be seen even in the exception message: + // Invocation was not performed on the mock: ifc => ifc.Get("XYZ") + // -----------------------------------------------------------^ + mock.Verify(ifc => ifc.Get(s)); + Assert.Equal(17, result); + } + + [Fact] + public void ImplicitSetupExplicitVerification_Fails() + { + const string s = "XYZ"; + var mock = new Mock(); + mock.Setup(ifc => ifc.Get(s)).Returns(17); + + var result = mock.Object.Get(s); + + // This verification passes oddly enough + mock.Verify(ifc => ifc.Get(SomeClass.From(s))); + + // This assert fails, indicating that the setup was not used + Assert.Equal(17, result); + } + } + } + + #endregion + + #region #146 + + public class _146 + { + public interface IFoo + { + bool Property { get; set; } + string StringProperty { get; set; } + } + + [Fact] + public void StrictMockPropertySet() + { + var mock = new Mock(MockBehavior.Strict); + + mock.SetupSet(v => v.Property = false); + + Assert.Throws(() => mock.VerifySet(v => v.Property = false)); + + mock.Object.Property = false; + + mock.VerifySet(v => v.Property = false); + } + } + + #endregion + + #region #158 + + public class _158 + { + public class Foo + { + public virtual void Boo() + { + Bar(); + Bar(); + } + + protected virtual void Bar() + { + } + } + +#pragma warning disable 618 + [Fact(Skip = "This setup doesn't make sense, and xUnit does not provide this message checking capability.")] + public void ShouldRenderCustomMessage() + { + var foo = new Mock { CallBase = true }; + foo.Protected().Setup("Bar").AtMostOnce().Verifiable("Hello"); + foo.Object.Boo(); + //Assert.Throws("Hello", () => foo.Object.Boo()); + } +#pragma warning restore 618 + } + + #endregion + + #region #160 + +#if !SILVERLIGHT + public class _160 + { + [Fact] + public void ShouldMockHtmlControl() + { + // CallBase was missing + var htmlInputTextMock = new Mock() { CallBase = true }; + Assert.True(htmlInputTextMock.Object.Visible); + } + } +#endif + + #endregion + + #region #161 + + public class _161 + { + [Fact] + public void InvertEqualObjects() + { + var foo1 = new Foo { Id = "1" }; + var foo = new Foo { Id = "2" }; + + var dependency = new Mock(); + + dependency.Setup(x => x.DoThis(foo, foo1)) + .Returns(new Foo()); + + var f = dependency.Object.DoThis(foo, foo1); + + dependency.Verify(x => x.DoThis(foo, foo1)); + dependency.Verify(x => x.DoThis(foo1, foo), Times.Never()); + } + + [Fact(Skip = "Wrong Equals implemention in the report. Won't Fix")] + public void ExampleFailingTest() + { + var foo1 = new Foo(); + var foo = new Foo(); + + var sut = new Perfectly_fine_yet_failing_test(); + var dependency = new Mock(); + + dependency.Setup(x => x.DoThis(foo, foo1)) + .Returns(new Foo()); + + sut.Do(dependency.Object, foo, foo1); + + dependency.Verify(x => x.DoThis(foo, foo1)); + dependency.Verify(x => x.DoThis(foo1, foo), Times.Never()); + } + + public class Perfectly_fine_yet_failing_test + { + public void Do(IDependency dependency, Foo foo, Foo foo1) + { + var foo2 = dependency.DoThis(foo, foo1); + if (foo2 == null) + foo2 = dependency.DoThis(foo1, foo); + } + } + + public interface IDependency + { + Foo DoThis(Foo foo, Foo foo1); + } + + public class Foo + { + public string Id { get; set; } + + public override bool Equals(object obj) + { + return obj is Foo && ((Foo)obj).Id == Id; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } + } + } + + #endregion + + #region #174 + + public class _174 + { + [Fact] + public void Test() + { + var serviceNo1Mock = new Mock(); + var collaboratorMock = new Mock(); + + collaboratorMock.Object.Collaborate(serviceNo1Mock.Object); + + collaboratorMock.Verify(o => o.Collaborate(serviceNo1Mock.Object)); + } + + public interface ISomeCollaborator + { + void Collaborate(IServiceNo1 serviceNo1); + } + + public interface IServiceNo1 : IEnumerable + { + } + } + + #endregion + + #region #177 + + public class _177 + { + [Fact] + public void Test() + { + var mock = new Mock(); + Assert.NotNull(mock.Object); + } + + public interface IMyInterface + { + void DoStuff() where TTo : TFrom; + } + } + + #endregion + + #region #184 + + public class _184 + { + [Fact] + public void Test() + { + var fooRaised = false; + var barRaised = false; + + var fooMock = new Mock(); + var barMock = fooMock.As(); + + fooMock.Object.FooEvent += (s, e) => fooRaised = true; + barMock.Object.BarEvent += (s, e) => barRaised = true; + + fooMock.Raise(m => m.FooEvent += null, EventArgs.Empty); + barMock.Raise(m => m.BarEvent += null, EventArgs.Empty); + + Assert.True(fooRaised); + Assert.True(barRaised); + } + + public interface IFoo + { + event EventHandler FooEvent; + } + + public interface IBar + { + event EventHandler BarEvent; + } + } + + #endregion + + #region #185 + + public class _185 + { + [Fact] + public void Test() + { + var mock = new Mock>(); + Assert.Throws(() => mock.Setup(l => l.FirstOrDefault()).Returns("Hello world")); + } + } + + #endregion + + #region #187 + + public class _187 + { + [Fact] + public void Test() + { + var mock = new Mock(); + + mock.Setup(r => r.Get()).Returns(new Object()); + mock.Setup(r => r.Get()).Returns(new Object()); + + Assert.NotNull(mock.Object.Get()); + Assert.NotNull(mock.Object.Get()); + } + + public class Foo + { + public class Inner + { + } + } + + public class Bar + { + public class Inner + { + } + } + + public interface IGeneric + { + object Get() where T : new(); + } + } + + #endregion + + #region #183 + + public class _183 + { + [Fact] + public void Test() + { + var mock = new Mock(); + mock.Setup(m => m.Execute(1)); + mock.Setup(m => m.Execute(It.IsInRange(2, 20, Range.Exclusive))); + mock.Setup(m => m.Execute(3, "Caption")); + + mock.Object.Execute(3); + mock.Object.Execute(4); + mock.Object.Execute(5); + + var e = Assert.Throws(() => mock.Verify(m => m.Execute(0))); + Assert.Contains( + "\r\nConfigured setups:" + + "\r\nm => m.Execute(1), Times.Never" + + "\r\nm => m.Execute(It.IsInRange(2, 20, Range.Exclusive)), Times.Exactly(3)", + e.Message); + } + + [Fact] + public void TestGeneric() + { + var mock = new Mock(); + mock.Setup(m => m.Execute(1, 10)); + mock.Setup(m => m.Execute(1, "Foo")); + + mock.Object.Execute(1, 10); + + var e = Assert.Throws(() => mock.Verify(m => m.Execute(1, 1))); + Assert.Contains( + "\r\nConfigured setups:\r\nm => m.Execute(1, 10), Times.Once", + e.Message); + } + + [Fact] + public void TestNoSetups() + { + var mock = new Mock(); + + var e = Assert.Throws(() => mock.Verify(m => m.Execute(1))); + Assert.Contains("\r\nNo setups configured.", e.Message); + + } + + public interface IFoo + { + void Execute(int param); + void Execute(int param, string caption); + void Execute(int p, T param); + } + } + + #endregion + + #region #186 + + public class _186 + { + [Fact] + public void TestVerifyMessage() + { + var mock = new Mock(); + mock.Setup(m => m.OnExecute()); + + var e = Assert.Throws(() => mock.Verify(m => m.Execute())); + Assert.True(e.Message.StartsWith("Invalid verify")); + } + + public class Foo + { + public void Execute() + { + this.OnExecute(); + } + + public virtual void OnExecute() + { + throw new NotImplementedException(); + } + } + } + + #endregion + + #region #190 + + public class _190 + { + [Fact] + public void Test() + { + var mock = new Mock().As(); + mock.SetupAllProperties(); + + ISite site = new FooSite(); + mock.Object.Site = site; + Assert.Same(site, mock.Object.Site); + } + + public class FooSite : ISite + { + public IComponent Component + { + get { throw new NotImplementedException(); } + } + + public IContainer Container + { + get { throw new NotImplementedException(); } + } + + public bool DesignMode + { + get { throw new NotImplementedException(); } + } + + public string Name + { + get { throw new NotImplementedException(); } + set { throw new NotImplementedException(); } + } + + public object GetService(Type serviceType) + { + throw new NotImplementedException(); + } + } + + } + + #endregion + + #region #204 + + public class _204 + { + [Fact] + public void Test() + { + var mock = new Mock(); + mock.Setup(x => x.Select(u => u.Id == 100)) + .Returns(new User() { Id = 100 }); + + var user = mock.Object.Select(usr => usr.Id == 100); + Assert.Equal(100, user.Id); + mock.Verify(x => x.Select(usr => usr.Id == 100), Times.Once()); + + user = mock.Object.Select(usr => usr.Id == 101); + Assert.Null(user); + mock.Verify(x => x.Select(usr => usr.Id == 101), Times.Once()); + + mock.Verify(x => x.Select(usr => usr.Id == 102), Times.Never()); + mock.Verify(x => x.Select(It.IsAny>>()), Times.Exactly(2)); + } + + public interface IRepository + { + T Select(Expression> filter) where T : class; + } + + public class User + { + public int Id { get; set; } + } + } + + #endregion + + #region #205 + + public class _205 + { + [Fact] + public void Test() + { + Assert.DoesNotThrow(() => new Mock().SetupAllProperties()); + } + + public interface IFoo + { + string Error { get; set; } + string this[int index] { get; set; } + } + } + + #endregion + + #region #223 + + public class _223 + { + [Fact] + public void TestSetup() + { + this.TestSetupHelper(); + } + + public void TestSetupHelper() where T : class, IFoo + { + var expected = 2; + + var target = new Mock(); + target.Setup(p => p.DoInt32(0)).Returns(expected); + target.Setup(p => p.DoGeneric(0)).Returns(expected); + + Assert.Equal(expected, target.Object.DoInt32(0)); + Assert.Equal(expected, target.Object.DoGeneric(0)); + } + + public interface IFoo + { + int DoInt32(int value); + T DoGeneric(int value); + } + + public class Foo : IFoo + { + public virtual int DoInt32(int value) + { + return 4; + } + + public virtual int DoGeneric(int value) + { + return 5; + } + } + } + + #endregion + + #region #229 + + public class _229 + { + [Fact] + public void Test() + { + var target = new Mock { CallBase = true }; + + var raised = false; + target.Object.MyEvent += (s, e) => raised = true; + target.Object.RaiseMyEvent(); + + Assert.True(raised); + } + + public class Foo + { + public virtual event EventHandler MyEvent; + + public void RaiseMyEvent() + { + if (this.MyEvent != null) + { + this.MyEvent(this, EventArgs.Empty); + } + } + } + } + + #endregion + + #region #230 + + public class _230 + { + [Fact] + public void ByteArrayCallbackArgumentShouldNotBeNull() + { + var data = new byte[] { 2, 1, 2 }; + var stream = new Mock(); + + stream.SetupGet(m => m.Length) + .Returns(data.Length); + stream.Setup(m => m.Read(It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((b, o, c) => data.CopyTo(b, 0)) + .Returns(data.Length); + + var contents = new byte[stream.Object.Length]; + Assert.DoesNotThrow(() => stream.Object.Read(contents, 0, (int)stream.Object.Length)); + } + } + + #endregion + + #region #232 + + public class _232 + { + [Fact] + public void Test() + { + var repository = new Mock(); + var svc = new Service(repository.Object); + + svc.Create(); + + repository.Verify(r => r.Insert(It.IsAny()), Times.Once()); + repository.Verify(r => r.Insert(It.IsAny()), Times.Once()); + repository.Verify(r => r.Insert(It.IsAny()), Times.Exactly(2)); + } + + public interface IRepository + { + void Insert(IEntity entity); + } + + public interface IEntity + { + } + + public class Foo : IEntity + { + } + + public class Bar : IEntity + { + } + + public class Service + { + private IRepository repository; + + public Service(IRepository repository) + { + this.repository = repository; + } + + public void Create() + { + repository.Insert(new Foo()); + repository.Insert(new Bar()); + } + } + } + + #endregion + + #region #242 + + public class _242 + { + [Fact] + public void PropertyChangedTest() + { + var mock = new Mock(); + int callbacks = 0; + mock.Object.PropertyChanged += (sender, args) => callbacks++; + + mock.Raise(m => m.PropertyChanged += null, new PropertyChangedEventArgs("Foo")); + Assert.Equal(1, callbacks); + } + + public class PropertyChangedBase : INotifyPropertyChanged + { + public virtual event PropertyChangedEventHandler PropertyChanged = (s, e) => { }; + } + + public class PropertyChangedInherited : PropertyChangedBase + { + } + } + + #endregion + + #region #245 + + public class _245 + { + [Fact] + public void Test() + { + var mock = new Mock(); + + ITest instance; + Assert.DoesNotThrow(() => instance = mock.Object); + } + + public interface ITest + { + void Do() where T2 : T1; + } + } + + #endregion + + #region #251 + + public class _251 + { + [Fact] + public void Test() + { + var repositoryMock = new Mock>(); + + var repository = repositoryMock.Object; + repository.Save("test"); + + repositoryMock.Verify(m => m.Save("test")); + } + + public interface IRepository + { + void Save(string value); + } + + public interface IRepository : IRepository + { + void Save(T value); + } + } + + #endregion + + #region #256 + + public class _256 + { + [Fact] + public void TestFinalizeNotMocked() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(m => m.Foo).Returns(10); + mock.Setup(m => m.Bar).Returns("Hello mocked world!"); + var instance = mock.Object; + + Assert.Equal(10, instance.Foo); + } + + public class ClassWithFinalizer + { + public virtual int Foo { get; set; } + public virtual string Bar { get; set; } + + ~ClassWithFinalizer() + { + + } + } + } + + #endregion + + #region #261 + + public class _261 + { + [Fact] + public void Test() + { + var mock = new Mock(); + mock.Protected().SetupSet("Status", 42); + + mock.Object.SetStatus(42); + + mock.Protected().VerifySet("Status", Times.Once(), 42); + } + + public class Foo + { + public virtual int Status { get; protected set; } + + internal void SetStatus(int value) + { + this.Status = value; + } + } + } + + #endregion + + #region #267 + + public class _267 + { + public interface IPerformOperation + { + string Operation(object input); + } + + public class OperationUser + { + private readonly IPerformOperation m_OperationPerformer; + + public OperationUser(IPerformOperation operationPerformer) + { + m_OperationPerformer = operationPerformer; + } + + public string DoOperation(object input) + { + return m_OperationPerformer.Operation(input); + } + } + + public class HelperSetup + { + private Mock m_OperationStub; + + public HelperSetup() + { + m_OperationStub = new Mock(); + } + + [Fact] + public void InlineSetupTest() + { + m_OperationStub.Setup(m => m.Operation(It.IsAny())).Returns(value => "test"); + m_OperationStub.Setup(m => m.Operation(It.IsAny())).Returns(value => "25"); + + var operationUser = new OperationUser(m_OperationStub.Object); + + var intOperationResult = operationUser.DoOperation(9); + var stringOperationResult = operationUser.DoOperation("Hello"); + + Assert.Equal("25", intOperationResult); + Assert.Equal("test", stringOperationResult); + } + + [Fact] + public void HelperSetupTest() + { + SetupOperationStub(value => "test"); + SetupOperationStub(value => "25"); + + var operationUser = new OperationUser(m_OperationStub.Object); + + var intOperationResult = operationUser.DoOperation(9); + var stringOperationResult = operationUser.DoOperation("Hello"); + + Assert.Equal("25", intOperationResult); + Assert.Equal("test", stringOperationResult); + } + + private void SetupOperationStub(Func valueFunction) + { + m_OperationStub.Setup(m => m.Operation(It.IsAny())).Returns(valueFunction); + } + } + } + + #endregion + + #region #273 + +#if !SILVERLIGHT + + public class _273 + { + [Fact] + public void WhenMockingAnExternalInterface_ThenItWorks() + { + Assert.NotNull(new Mock().Object); + Assert.NotNull(Mock.Of()); + Assert.NotNull(new Mock().Object); + Assert.NotNull(new Mock().Object); + Assert.NotNull(Mock.Of()); + Assert.NotNull(new Mock().Object); + Assert.NotNull(new Mock().Object); + } + + public class Baz : ClassLibrary2.Bar + { + } + } + +#endif + + #endregion + + #region #325 + + public class _325 + { + [Fact] + public void SubscribingWorks() + { + var target = new Mock { CallBase = true }; + target.As(); + + var bar = (IBar)target.Object; + var raised = false; + bar.SomeEvent += (sender, e) => raised = true; + + target.As().Raise(b => b.SomeEvent += null, EventArgs.Empty); + + Assert.True(raised); + } + + [Fact] + public void UnsubscribingWorks() + { + var target = new Mock { CallBase = true }; + target.As(); + + var bar = (IBar)target.Object; + var raised = false; + EventHandler handler = (sender, e) => raised = true; + bar.SomeEvent += handler; + bar.SomeEvent -= handler; + + target.As().Raise(b => b.SomeEvent += null, EventArgs.Empty); + + Assert.False(raised); + } + + public class Foo + { + } + + public interface IBar + { + event EventHandler SomeEvent; + } + + } + + #endregion + + #region Recursive issue + + public class RecursiveFixture + { + [Fact] + public void TestRecursive() + { + var mock = new Mock() { DefaultValue = DefaultValue.Mock }; + mock.Setup(c => c.HttpContext.Response.Write("stuff")); + + mock.Object.HttpContext.Response.Write("stuff"); + mock.Object.HttpContext.Response.ShouldEncode = true; + + Assert.Throws(() => mock.VerifySet( + c => c.HttpContext.Response.ShouldEncode = It.IsAny(), + Times.Never())); + } + + public class ControllerContext + { + public virtual HttpContext HttpContext { get; set; } + } + + public abstract class HttpContext + { + protected HttpContext() + { + } + + public virtual HttpResponse Response + { + get { throw new NotImplementedException(); } + } + } + + public abstract class HttpResponse + { + protected HttpResponse() + { + } + + public virtual bool ShouldEncode + { + get { throw new NotImplementedException(); } + set { throw new NotImplementedException(); } + } + + public virtual void Write(string s) + { + throw new NotImplementedException(); + } + } + } + + #endregion + + #region Silverlight excluded + +#if !SILVERLIGHT + + #region #250 + + /// + /// Silverlight MethodInfo protected constructor is internal, unlike desktop .NET + /// + public class _250 + { + [Fact] + public void Test() + { + var target = new Mock(); + + Assert.NotNull(target.Object); + } + } + + #endregion + + #region #250 + + public class _254 + { + [Fact] + public void ShouldMockInteropDTE() + { + var dte = new Mock(); + + Assert.NotNull(dte.Object); + } + +#if !NET3x && !SERVER + [Fact] + public void ShouldRaiseEventOnInteropInterface() + { + var app = new Mock(); + var count = 0; + app.Object.DocumentOpen += doc => count++; + + app.Raise(x => x.DocumentOpen += null, new Mock().Object); + + Assert.Equal(1, count); + } +#endif + } + + #endregion + + // run "netsh http add urlacl url=http://+:7777/ user=[domain]\[user]" + // to avoid running the test as an admin + [Fact(Skip = "Doesn't work in Mono")] + public void ProxiesAndHostsWCF() + { +#if DEBUG + // On release mode, castle is ILMerged into Moq.dll and this won't compile + var generator = new Castle.DynamicProxy.ProxyGenerator(); + var proxy = generator.CreateClassProxy(); + using (var host = new WebServiceHost(proxy, new Uri("http://localhost:7777"))) + { + host.Open(); + } +#endif + } + + // run "netsh http add urlacl url=http://+:7777/ user=[domain]\[user]" + // to avoid running the test as an admin + [Fact(Skip = "Doesn't work in Mono")] + public void ProxiesAndHostsWCFMock() + { + //var generator = new Castle.DynamicProxy.ProxyGenerator(); + var proxy = new Mock(); + using (var host = new WebServiceHost(proxy.Object, new Uri("http://localhost:7777"))) + { + host.Open(); + } + } + + [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] + public class ServiceImplementation : IServiceContract + { + public void Do() + { + throw new NotImplementedException(); + } + } + + [ServiceContract] + public interface IServiceContract + { + [OperationContract] + void Do(); + } + +#endif + + #endregion + } } \ No newline at end of file diff --git a/UnitTests/packages.config b/UnitTests/packages.config new file mode 100644 index 000000000..227489518 --- /dev/null +++ b/UnitTests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/moq-bigger.png b/moq-bigger.png new file mode 100644 index 000000000..085b792b6 Binary files /dev/null and b/moq-bigger.png differ