|
From: Levi K. <lkh...@us...> - 2004-08-24 21:15:54
|
Update of /cvsroot/nmock/nmock/test/NMock In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25016/test/NMock Modified Files: DynamicMockTest.cs Added Files: ProxyMockTest.cs TypeCheckedMockTest.cs Log Message: Added ProxyMock: can mock interface and MarshalByRefObjects, can mock sealed classes/non-empty contructor and nonvirtual members; related refactoring; some MethodSignature methods moved to a new class --- NEW FILE: TypeCheckedMockTest.cs --- using System; using System.Collections; using System.Reflection; using NMock.Constraints; using NUnit.Framework; namespace NMock { public abstract class TypeCheckedMockTest : Assertion { #region types public interface IBaseBlah { string GetSuperString(); } public interface IBlah : IBaseBlah { object DoStuff(string name); } public class SameClass : MarshalByRefObject { public virtual string A() { return c() ? b() : "Default"; } protected virtual string b() { throw new Exception("Should not have called b()"); } protected virtual bool c() { return true; } } public interface IValueType { ArrayList Query(string symbol, DateTime arg2); } public class Thingy : MarshalByRefObject { public void X() {} } public interface IOverloadedMethods { void DoStuff(string one, int two); void DoStuff(string one); } public interface TwoMethods { void Foo(String a); int Bar(String a); } public interface IWithParams { void WithLeadingParameter(int i, params object[] args); void WithoutLeadingParameter(params object[] args); } public class WithNonEmptyConstructor : MarshalByRefObject { public WithNonEmptyConstructor(string unused) { } } public interface IWithProperty { string Name { get; set; } } public class WithNonVirtualMethod : MarshalByRefObject { public void NonVirtualMethod() { } } public interface WithDifferentTypesOfParameters { void SomeParametersMethod(string p1, string p2); } public interface WithMethodThatReturnsAMock { Mock ReturnIt(); } #endregion protected abstract IMock CreateMock(Type type); [Test] public void HasADefaultNameBasedOnMockedType() { IMock mock = CreateMock(typeof(IBlah)); Assertion.AssertEquals("MockIBlah", mock.Name); } [Test] public void DynamicallyImplementsAnInterface() { IMock mock = CreateMock(typeof(IBlah)); mock.ExpectAndReturn("DoStuff", "world", "hello"); IBlah blah = (IBlah)mock.MockInstance; Assertion.AssertEquals("world", blah.DoStuff("hello")); mock.Verify(); } [Test] public void SetupResultCanReturnAssignableTypesAsWellAsTheSameType() { IMock mock = CreateMock(typeof(WithMethodThatReturnsAMock)); mock.SetupResult("ReturnIt", CreateMock(typeof(MarshalByRefObject))); ((WithMethodThatReturnsAMock)mock.MockInstance).ReturnIt(); } [Test] public void InvocationFailsIfParamaterValueIsIncorrect() { IMock m1 = CreateMock(typeof(Thingy)); Thingy thingy = (Thingy)m1.MockInstance; IMock m2 = new Mock("x"); m2.Expect("y", thingy); try { m2.Invoke("y", "something else"); } catch (VerifyException) { return; } Fail("Should have thrown VerifyException"); } [Test] public void CanExpectMultipleInputsAndReturnAValue() { IMock mock = CreateMock(typeof(IValueType)); ArrayList ret = new ArrayList(); DateTime date = DateTime.Now; mock.ExpectAndReturn("Query", ret, "hello", date); IValueType blah = (IValueType)mock.MockInstance; Assertion.AssertEquals(ret, blah.Query("hello", date)); mock.Verify(); } [Test] public void CanExpectFromMembersInheritedFromBaseInterfaces() { IMock mock = CreateMock(typeof(IBlah)); mock.ExpectAndReturn("GetSuperString", "some string"); IBlah b = (IBlah) mock.MockInstance; Assertion.AssertEquals("some string", b.GetSuperString()); mock.Verify(); } [Test] public void CanSetupResultForMembersInheritedFromBaseInterfaces() { IMock mock = CreateMock(typeof(IBlah)); mock.SetupResult("GetSuperString", "some string"); IBlah b = (IBlah) mock.MockInstance; Assertion.AssertEquals("some string", b.GetSuperString()); mock.Verify(); } [Test] public void CanMockMembersWithAParamsArgument() { IMock mock = CreateMock(typeof(IWithParams)); mock.Expect("WithLeadingParameter", 1, new Object[] {1, 2, 3}); IWithParams p = (IWithParams)mock.MockInstance; p.WithLeadingParameter(1, 1, 2, 3); mock.Verify(); } [Test] public void CannotYetMockMembersWithOnlyAParamsArgument() { IMock mock = CreateMock(typeof(IWithParams)); try { mock.Expect("WithoutLeadingParameter", new Object[] {1, 2, 3}); Fail("Should have thrown MissingMethodException"); } catch (MissingMethodException) {} } [Test] public void CanMockOverloadedMethods() { IMock mock = CreateMock(typeof(IOverloadedMethods)); mock.Expect("DoStuff", "one", 2); mock.ExpectNoCall("DoStuff", typeof(string)); IOverloadedMethods instance = (IOverloadedMethods)mock.MockInstance; instance.DoStuff("one", 2); mock.Verify(); } [Test] public void CanMockMethodWithConstraint() { IMock mock = CreateMock(typeof(TwoMethods)); mock.Expect("Foo", new StartsWith("Hello")); mock.ExpectAndReturn("Bar", 5, new NotNull()); TwoMethods instance = (TwoMethods)mock.MockInstance; instance.Foo("Hello World"); Assertion.AssertEquals("Should get a result", 5, instance.Bar("not null")); mock.Verify(); } [Test] public void CanSetAndGetPropertiesOnAMockedInterface() { IMock mock = CreateMock(typeof(IWithProperty)); IWithProperty withProperty = (IWithProperty)mock.MockInstance; mock.ExpectAndReturn("Name", "fred"); mock.Expect("Name", "joe"); AssertEquals("Should be property Name", "fred", withProperty.Name); withProperty.Name = "joe"; mock.Verify(); } [Test] public void SetAndGetPropertiesDoesNotWorkWithSetupReturn() { IMock mock = CreateMock(typeof(IWithProperty)); IWithProperty withProperty = (IWithProperty)mock.MockInstance; mock.SetupResult("Name", "fred"); mock.Expect("Name", "jim"); try { AssertEquals("Should be property Name", "fred", withProperty.Name); withProperty.Name = "jim"; mock.Verify(); } catch (VerifyException) { return; } Fail("Should have thrown VerifyException"); } [Test] public void TestNullParameterCanStillResolveCorrectMethod() { IMock mock = CreateMock(typeof(WithDifferentTypesOfParameters)); WithDifferentTypesOfParameters instance = (WithDifferentTypesOfParameters)mock.MockInstance; //works mock.Expect("SomeParametersMethod", "foo", "foo"); instance.SomeParametersMethod("foo", "foo"); //fails mock.Expect("SomeParametersMethod", "foo", null); instance.SomeParametersMethod("foo", null); mock.Verify(); } [Test] public void TestNullConstraintsCanStillResolveCorrectMethod() { IMock mock = CreateMock(typeof(WithDifferentTypesOfParameters)); WithDifferentTypesOfParameters instance = (WithDifferentTypesOfParameters)mock.MockInstance; //works mock.Expect("SomeParametersMethod", "foo", "foo"); instance.SomeParametersMethod("foo", "foo"); //bug - cannot resolve this mock.Expect("SomeParametersMethod", "foo", new IsNull()); instance.SomeParametersMethod("foo", null); mock.Verify(); } } } --- NEW FILE: ProxyMockTest.cs --- using System; using NUnit.Framework; namespace NMock { [TestFixture] public class ProxyMockTest : TypeCheckedMockTest { #region types public class CustomMock : ProxyMock { public CustomMock(Type t) : base(t) {} public override object Invoke(string name, params object[] args) { return "CUSTOM"; } } public class NonMarshalByRef { } public sealed class SealedThingy : MarshalByRefObject { } #endregion protected override IMock CreateMock(Type type) { return new ProxyMock(type); } [Test] public void CanBeExplicitlyNamed() { IMock mock = new ProxyMock(typeof(IBlah), "XBlah"); Assertion.AssertEquals("XBlah", mock.Name); } [Test] public void CannotSetStubsAndExpectationsOnMethodsInTheSameClass() { ProxyMock mock = new ProxyMock(typeof(SameClass)); SameClass sc = (SameClass)mock.MockInstance; mock.ExpectAndReturn("c", true); mock.SetupResult("b", "hello"); AssertEquals("Should not have overriden B()", null, sc.A()); } [Test] public void CanBeCustomisedByOverridingCallMethod() { IMock mock = new CustomMock(typeof(IBlah)); IBlah blah = (IBlah)mock.MockInstance; Assertion.AssertEquals("CUSTOM", blah.DoStuff("hello")); mock.Verify(); } [Test] public void NamedProxyMockImplementsAnInterface() { IMock mock = new ProxyMock(typeof(IBlah), "XBlah"); mock.ExpectAndReturn("DoStuff", "world", "hello"); IBlah blah = (IBlah)mock.MockInstance; Assertion.AssertEquals("world", blah.DoStuff("hello")); mock.Verify(); } [Test] public void StubbedMethodsCannotBeCalledByOtherMethodsWithinObject() { ProxyMock mock = new ProxyMock(typeof(SameClass)); SameClass sc = (SameClass)mock.MockInstance; mock.SetupResult("b", "hello"); Assertion.AssertEquals(null, sc.A()); } [Test] public void MockInstanceCanBeUsedAsValueInAnExpectation() { IMock mockThingy = new ProxyMock(typeof(Thingy)); Thingy thingy = (Thingy)mockThingy.MockInstance; mockThingy.SetupResult("Equals", true, typeof(object)); IMock m2 = new Mock("x"); m2.Expect("y", thingy); m2.Invoke("y", thingy); m2.Verify(); } [Test] public void CanMockNonVirtualMethodsOfAClass() { IMock mock = new ProxyMock(typeof(WithNonVirtualMethod)); mock.Expect("NonVirtualMethod"); } [Test] public void CanCreateMockInstanceWithNonEmptyConstructor() { IMock mock = new ProxyMock(typeof(WithNonEmptyConstructor)); WithNonEmptyConstructor nonEmpty = (WithNonEmptyConstructor)mock.MockInstance; } [ExpectedExceptionAttribute(typeof(ArgumentException))] [Test] public void CannotMockNonMarshalByRefClass() { new ProxyMock(typeof(NonMarshalByRef)); } [Test] public void CanMockSealedClass() { new ProxyMock(typeof(SealedThingy)); } } } Index: DynamicMockTest.cs =================================================================== RCS file: /cvsroot/nmock/nmock/test/NMock/DynamicMockTest.cs,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** DynamicMockTest.cs 24 Aug 2004 16:00:24 -0000 1.23 --- DynamicMockTest.cs 24 Aug 2004 21:15:30 -0000 1.24 *************** *** 1,25 **** - using NUnit.Framework; using System; - using System.Collections; using System.Reflection; ! using NMock.Constraints; namespace NMock { ! [TestFixture] public class DynamicMockTest : Assertion { #region types ! interface IBaseBlah ! { ! string GetSuperString(); ! } ! ! interface IBlah : IBaseBlah ! { ! object DoStuff(string name); ! } ! ! private class CustomMock : DynamicMock { public CustomMock(Type t) : base(t) {} --- 1,14 ---- using System; using System.Reflection; ! ! using NUnit.Framework; namespace NMock { ! [TestFixture] public class DynamicMockTest : TypeCheckedMockTest { #region types ! public class CustomMock : DynamicMock { public CustomMock(Type t) : base(t) {} *************** *** 30,99 **** } } - public class SameClass - { - public virtual string A() { return c() ? b() : "Default"; } - protected virtual string b() { throw new Exception("Should not have called b()"); } - protected virtual bool c() { return true; } - } - - interface IValueType - { - ArrayList Query(string symbol, DateTime arg2); - } - - public class Thingy - { - public void X() {} - } - interface IOverloadedMethods - { - void DoStuff(string one, int two); - void DoStuff(string one); - } - interface TwoMethods - { - void Foo(String a); - int Bar(String a); - } - interface IWithParams - { - void WithLeadingParameter(int i, params object[] args); - void WithoutLeadingParameter(params object[] args); - } - class WithNonEmptyConstructor - { - public WithNonEmptyConstructor(string unused) - { - } - } - interface IWithProperty - { - string Name { get; set; } - } - class WithNonVirtualMethod - { - public void NonVirtualMethod() { } - } - interface WithDifferentTypesOfParameters - { - void SomeParametersMethod(string p1, string p2); - } - - interface WithMethodThatReturnsAMock - { - Mock ReturnIt(); - } public class DisposableThingy : Thingy { } #endregion ! [Test] public void HasADefaultNameBasedOnMockedType() { ! IMock mock = new DynamicMock(typeof(IBlah)); ! Assertion.AssertEquals("MockIBlah", mock.Name); } [Test] public void CanBeExplicitlyNamed() { --- 19,35 ---- } } public class DisposableThingy : Thingy { } + #endregion ! protected override IMock CreateMock(Type type) { ! return new DynamicMock(type); } + [Test] public void CanBeExplicitlyNamed() { *************** *** 102,114 **** } ! [Test] public void DynamicallyImplementsAnInterface() { ! IMock mock = new DynamicMock(typeof(IBlah)); ! ! mock.ExpectAndReturn("DoStuff", "world", "hello"); ! IBlah blah = (IBlah)mock.MockInstance; ! Assertion.AssertEquals("world", blah.DoStuff("hello")); ! mock.Verify(); } --- 38,60 ---- } ! [Test] public void CanSetStubsAndExpectationsOnMethodsInTheSameClass() { ! DynamicMock mock = new DynamicMock(typeof(SameClass)); ! mock.Ignore("A"); ! SameClass sc = (SameClass)mock.MockInstance; ! ! mock.ExpectAndReturn("c", true); ! mock.SetupResult("b", "hello"); ! ! AssertEquals("Should have overriden B()", "hello", sc.A()); ! ! mock.Verify(); ! } ! ! [Test] public void CanBeCustomisedByOverridingCallMethod() ! { ! IMock mock = new CustomMock(typeof(IBlah)); IBlah blah = (IBlah)mock.MockInstance; ! Assertion.AssertEquals("CUSTOM", blah.DoStuff("hello")); mock.Verify(); } *************** *** 126,137 **** } - [Test] public void CanBeCustomisedByOverridingCallMethod() - { - IMock mock = new CustomMock(typeof(IBlah)); - IBlah blah = (IBlah)mock.MockInstance; - Assertion.AssertEquals("CUSTOM", blah.DoStuff("hello")); - mock.Verify(); - } - [Test] public void StubbedMethodsCanBeCalledByOtherMethodsWithinObject() { --- 72,75 ---- *************** *** 146,176 **** } - [Test] public void SetupResultCanReturnAssignableTypesAsWellAsTheSameType() - { - DynamicMock mock = new DynamicMock(typeof(WithMethodThatReturnsAMock)); - - mock.SetupResult("ReturnIt", new DynamicMock(typeof(object))); - - ((WithMethodThatReturnsAMock)mock.MockInstance).ReturnIt(); - } - [Test] public void CanSetStubsAndExpectationsOnMethodsInTheSameClass() - { - DynamicMock mock = new DynamicMock(typeof(SameClass)); - mock.Ignore("A"); - SameClass sc = (SameClass)mock.MockInstance; - - mock.ExpectAndReturn("c", true); - mock.SetupResult("b", "hello"); - - AssertEquals("Should have overriden B()", "hello", sc.A()); - - mock.Verify(); - } - [Test] public void MockInstanceCanBeUsedAsValueInAnExpectation() { ! DynamicMock mockThingy = new DynamicMock(typeof(Thingy)); Thingy thingy = (Thingy)mockThingy.MockInstance; ! Mock m2 = new Mock("x"); m2.Expect("y", thingy); --- 84,92 ---- } [Test] public void MockInstanceCanBeUsedAsValueInAnExpectation() { ! IMock mockThingy = new DynamicMock(typeof(Thingy)); Thingy thingy = (Thingy)mockThingy.MockInstance; ! IMock m2 = new Mock("x"); m2.Expect("y", thingy); *************** *** 179,258 **** } ! [Test] public void InvocationFailsIfParamaterValueIsIncorrect() ! { ! DynamicMock m1 = new DynamicMock(typeof(Thingy)); ! Thingy thingy = (Thingy)m1.MockInstance; ! Mock m2 = new Mock("x"); ! ! m2.Expect("y", thingy); ! try ! { ! m2.Invoke("y", "something else"); ! } ! catch (VerifyException) ! { ! return; ! } ! Fail("Should have thrown VerifyException"); ! } ! ! [Test] public void CanExpectMultipleInputsAndReturnAValue() ! { ! IMock mock = new DynamicMock(typeof(IValueType)); ! ArrayList ret = new ArrayList(); ! DateTime date = DateTime.Now; ! mock.ExpectAndReturn("Query", ret, "hello", date); ! ! IValueType blah = (IValueType)mock.MockInstance; ! Assertion.AssertEquals(ret, blah.Query("hello", date)); ! ! mock.Verify(); ! } ! ! [Test] public void CanExpectFromMembersInheritedFromBaseInterfaces() ! { ! IMock mock = new DynamicMock(typeof(IBlah)); ! mock.ExpectAndReturn("GetSuperString", "some string"); ! ! IBlah b = (IBlah) mock.MockInstance; ! ! Assertion.AssertEquals("some string", b.GetSuperString()); ! mock.Verify(); ! } ! ! [Test] public void CanSetupResultForMembersInheritedFromBaseInterfaces() ! { ! IMock mock = new DynamicMock(typeof(IBlah)); ! mock.SetupResult("GetSuperString", "some string"); ! ! IBlah b = (IBlah) mock.MockInstance; ! ! Assertion.AssertEquals("some string", b.GetSuperString()); ! mock.Verify(); ! } ! ! [Test] public void CanMockMembersWithAParamsArgument() ! { ! IMock mock = new DynamicMock(typeof(IWithParams)); ! mock.Expect("WithLeadingParameter", 1, new Object[] {1, 2, 3}); ! ! IWithParams p = (IWithParams)mock.MockInstance; ! p.WithLeadingParameter(1, 1, 2, 3); ! mock.Verify(); ! } ! ! [Test] public void CannotYetMockMembersWithOnlyAParamsArgument() ! { ! IMock mock = new DynamicMock(typeof(IWithParams)); ! try ! { ! mock.Expect("WithoutLeadingParameter", new Object[] {1, 2, 3}); ! Fail("Should have thrown MissingMethodException"); ! } ! catch (MissingMethodException) ! {} ! } ! ! [Test] public void CannotMockNonVirtualMethodsOfAClass() { IMock mock = new DynamicMock(typeof(WithNonVirtualMethod)); --- 95,99 ---- } ! [Test] public void CannotMockNonVirtualMethodsOfAClass() { IMock mock = new DynamicMock(typeof(WithNonVirtualMethod)); *************** *** 266,297 **** } ! [Test] public void CanMockOverloadedMethods() ! { ! IMock mock = new DynamicMock(typeof(IOverloadedMethods)); ! mock.Expect("DoStuff", "one", 2); ! mock.ExpectNoCall("DoStuff", typeof(string)); ! ! IOverloadedMethods instance = (IOverloadedMethods)mock.MockInstance; ! instance.DoStuff("one", 2); ! ! mock.Verify(); ! } ! ! [Test] public void CanMockMethodWithConstraint() ! { ! IMock mock = new DynamicMock(typeof(TwoMethods)); ! mock.Expect("Foo", new StartsWith("Hello")); ! mock.ExpectAndReturn("Bar", 5, new NotNull()); ! ! TwoMethods instance = (TwoMethods)mock.MockInstance; ! instance.Foo("Hello World"); ! Assertion.AssertEquals("Should get a result", 5, instance.Bar("not null")); ! ! mock.Verify(); ! } ! ! [Test] public void CannotCreateMockInstanceWithNonEmptyConstructor() { ! IMock mock = new DynamicMock(typeof(WithNonEmptyConstructor)); try { --- 107,113 ---- } ! [Test] public void CannotCreateMockInstanceWithNonEmptyConstructor() { ! IMock mock = CreateMock(typeof(WithNonEmptyConstructor)); try { *************** *** 301,372 **** catch (NotSupportedException) {} } - - [Test] public void CanSetAndGetPropertiesOnAMockedInterface() - { - DynamicMock mock = new DynamicMock(typeof(IWithProperty)); - IWithProperty withProperty = (IWithProperty)mock.MockInstance; - - mock.ExpectAndReturn("Name", "fred"); - mock.Expect("Name", "joe"); - - AssertEquals("Should be property Name", "fred", withProperty.Name); - withProperty.Name = "joe"; - - mock.Verify(); - } - - [Test] public void SetAndGetPropertiesDoesNotWorkWithSetupReturn() - { - DynamicMock mock = new DynamicMock(typeof(IWithProperty)); - IWithProperty withProperty = (IWithProperty)mock.MockInstance; - - mock.SetupResult("Name", "fred"); - mock.Expect("Name", "jim"); - - try - { - AssertEquals("Should be property Name", "fred", withProperty.Name); - withProperty.Name = "jim"; - mock.Verify(); - } - catch (VerifyException) - { - return; - } - - Fail("Should have thrown VerifyException"); - } - - [Test] public void TestNullParameterCanStillResolveCorrectMethod() - { - DynamicMock mock = new DynamicMock(typeof(WithDifferentTypesOfParameters)); - WithDifferentTypesOfParameters instance = (WithDifferentTypesOfParameters)mock.MockInstance; - - //works - mock.Expect("SomeParametersMethod", "foo", "foo"); - instance.SomeParametersMethod("foo", "foo"); - - //fails - mock.Expect("SomeParametersMethod", "foo", null); - instance.SomeParametersMethod("foo", null); - - mock.Verify(); - } - - [Test] public void TestNullConstraintsCanStillResolveCorrectMethod() - { - DynamicMock mock = new DynamicMock(typeof(WithDifferentTypesOfParameters)); - WithDifferentTypesOfParameters instance = (WithDifferentTypesOfParameters)mock.MockInstance; - - //works - mock.Expect("SomeParametersMethod", "foo", "foo"); - instance.SomeParametersMethod("foo", "foo"); - - //bug - cannot resolve this - mock.Expect("SomeParametersMethod", "foo", new IsNull()); - instance.SomeParametersMethod("foo", null); - - mock.Verify(); - } [Test] public void VerifyThatFinalizeIsNotOverriddenInMockSubClass() --- 117,120 ---- *************** *** 381,385 **** IMock mock = new DynamicMock(typeof(DisposableThingy)); mock.Strict = true; ! return (DisposableThingy) mock.MockInstance; } } --- 129,133 ---- IMock mock = new DynamicMock(typeof(DisposableThingy)); mock.Strict = true; ! return (DisposableThingy)mock.MockInstance; } } |