From: Josh M. <jos...@gm...> - 2005-07-15 00:21:15
|
All - We've made three changes to the code we got out of the cvs repository. The following are patches for these changes (courtesy local svn) with commentary: 1) The first change simply allows the syntactic sugar of IFoo mockFoo =3D mockery.NewMock<IFoo>(). This should be the only .NET 2.0 dependent change (eyeball check - we haven't compiled under 1.1). -----clip here----- Index: C:/projects/nmock2/src/NMock2.Test/MockeryTest.cs =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- C:/projects/nmock2/src/NMock2.Test/MockeryTest.cs=09(revision 3) +++ C:/projects/nmock2/src/NMock2.Test/MockeryTest.cs=09(revision 4) @@ -42,7 +42,13 @@ =09=09=09object mock =3D mockery.NewMock(typeof (IMockedType)); =20 =09=09=09Assert.IsTrue(mock is IMockedType, "should be instance of mocked = type"); -=09=09} +=09=09} + + [Test] + public void CanCreateMockWithoutCastingBySpecifingTypeAsGenericParameter() + { + IMockedType mock =3D mockery.NewMock<IMockedType>(); + } =20 =09=09[Test] =09=09public void CreatesMocksThatCanBeCastToIMockObject() Index: C:/projects/nmock2/src/NMock2/Mockery.cs =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- C:/projects/nmock2/src/NMock2/Mockery.cs=09(revision 3) +++ C:/projects/nmock2/src/NMock2/Mockery.cs=09(revision 4) @@ -23,7 +23,12 @@ public object NewMock(Type mockedType) =09=09{ =09=09=09return NewMock(mockedType, DefaultNameFor(mockedType)); -=09=09} +=09=09} + + public InterfaceOfMock NewMock<InterfaceOfMock>() + { + return (InterfaceOfMock)NewMock(typeof(InterfaceOfMock)); + } =09=09 =09=09public object NewMock(Type mockedType, string name) =09=09{ -----clip here----- 2) This patch is to fix Expect.Exactly(N) - we haven't dug into figuring out why the initial implementation (using Is.EqualTo(count)) didn't work, but just made the new tests pass. -----clip here----- Index: C:/projects/nmock2/src/NMock2.AcceptanceTests/ExpectationsAcceptance= Test.cs =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- C:/projects/nmock2/src/NMock2.AcceptanceTests/ExpectationsAcceptanceTes= t.cs=09(revision 4) +++ C:/projects/nmock2/src/NMock2.AcceptanceTests/ExpectationsAcceptanceTes= t.cs=09(revision 5) @@ -40,8 +40,45 @@ =20 =09=09=09helloWorld.Hello(); =09=09=09helloWorld.Hello(); -=09=09} - +=09=09} + + + + [Test] + public void TestShouldPassIfExpectedExactlyNTimesAndCalledNTimes() + { + Mockery mocks =3D new Mockery(); + IHelloWorld helloWorld =3D (IHelloWorld)mocks.NewMock(typeof(IHelloWorld)); + Expect.Exactly(N).On(helloWorld).Method("Hello").WithNoArgumen= ts(); + + for (int i =3D 0; i < N; i++) helloWorld.Hello(); + + mocks.VerifyAllExpectationsHaveBeenMet(); + } + + [Test, ExpectedException(typeof(ExpectationException))] + public void TestShouldFailIfExpectedExactlyNTimesAndCalledMoreThanNTimes() + { + Mockery mocks =3D new Mockery(); + IHelloWorld helloWorld =3D (IHelloWorld)mocks.NewMock(typeof(IHelloWorld)); + Expect.Exactly(N).On(helloWorld).Method("Hello").WithNoArgumen= ts(); + + for (int i =3D 0; i < N+1; i++) helloWorld.Hello(); + + mocks.VerifyAllExpectationsHaveBeenMet(); + } + + [Test, ExpectedException(typeof(ExpectationException))] + public void TestShouldFailIfExpectedExactlyNTimesAndCalledLessThanNTimes() + { + Mockery mocks =3D new Mockery(); + IHelloWorld helloWorld =3D (IHelloWorld)mocks.NewMock(typeof(IHelloWorld)); + Expect.Exactly(N).On(helloWorld).Method("Hello").WithNoArgumen= ts(); + + for (int i =3D 0; i < N - 1; i++) helloWorld.Hello(); + + mocks.VerifyAllExpectationsHaveBeenMet(); + } =09=09[Test, ExpectedException(typeof(ExpectationException))] =09=09public void FailsTestIfMethodExpectedAtLeastNTimesButCalledLessThanN= Times() =09=09{ Index: C:/projects/nmock2/src/NMock2/Expect.cs =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- C:/projects/nmock2/src/NMock2/Expect.cs=09(revision 4) +++ C:/projects/nmock2/src/NMock2/Expect.cs=09(revision 5) @@ -24,8 +24,9 @@ =09=09} =09=09 =09=09public static IReceiverSyntax Exactly(int count) -=09=09{ - return new ExpectationBuilder(Times(count), Is.EqualTo(count), Is.EqualTo(count)); +=09=09{ + + return new ExpectationBuilder(Times(count), Is.AtLeast(count), Is.AtMost(count)); } =09=09 =09=09public static IReceiverSyntax AtLeast(int count) -----clip here----- 3) This allows use of Expect() with methods and properties on inherited interfaces (not just the interface passed to NewMock): -----clip here----- Index: C:/projects/nmock2/src/NMock2.Test/MockeryTest.cs =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- C:/projects/nmock2/src/NMock2.Test/MockeryTest.cs=09(revision 7) +++ C:/projects/nmock2/src/NMock2.Test/MockeryTest.cs=09(revision 9) @@ -303,9 +303,33 @@ =20 =09=09=09=09=09e.Message); =09=09=09} -=09=09} -=09} -=09 +=09=09} + + public interface IParentInterface + { + void DoSomething(); + } + + public interface IChildInterface : IParentInterface + { + + } + + [Test] + public void ShouldBeAbleToInvokeMethodOnInheritedInterface() + { + Mockery mockery =3D new Mockery(); + IChildInterface childMock =3D (IChildInterface)mockery.NewMock(typeof(IChildInterface)); + + Expect.AtLeastOnce.On(childMock).Method("DoSomething"); + + childMock.DoSomething(); + + mockery.VerifyAllExpectationsHaveBeenMet(); + } +=09} + + =09class MockExpectation : IExpectation =09{ =09=09public object ExpectedInvokedObject =3D null; Index: C:/projects/nmock2/src/NMock2/Mockery.cs =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- C:/projects/nmock2/src/NMock2/Mockery.cs=09(revision 7) +++ C:/projects/nmock2/src/NMock2/Mockery.cs=09(revision 9) @@ -1,7 +1,8 @@ using System; using System.Reflection; using NMock2.Internal; -using NMock2.Monitoring; +using NMock2.Monitoring; +using System.Collections; =20 namespace NMock2 { @@ -61,14 +62,36 @@ =09=09=09} =09=09} =09=09 -=09=09private bool TypeHasMethodMatching(Type type, Matcher matcher) -=09=09{ -=09=09=09foreach (MethodInfo method in type.GetMethods()) -=09=09=09{ -=09=09=09=09if (matcher.Matches(method)) return true; -=09=09=09} +=09=09private bool TypeHasMethodMatching(Type type, Matcher matcher) +=09=09{ + + foreach (Type implementedInterface in GetInterfacesImplementedByType(type)) + { + foreach (MethodInfo method in implementedInterface.GetMethods()) + { + + if (matcher.Matches(method)) return true; + + } + } +=09=09=09 =09=09=09return false; -=09=09} +=09=09} + + private Type[] GetInterfacesImplementedByType(Type type) + { + ArrayList implementedTypes =3D new ArrayList(); + foreach (Type implementedInterface in type.GetInterfaces()) + { + =20 implementedTypes.AddRange(GetInterfacesImplementedByType(implementedInterfa= ce)); + } + implementedTypes.Add(type); + + Type[] types =3D new Type[implementedTypes.Count]; + implementedTypes.CopyTo(types); + + return types; + } =09=09 =09=09public IDisposable Ordered =09=09{ -----clip here----- We used the built in migration wizzer for moving the 2003 sln and csproj files to 2005beta2 - it's a bit flakey, but we made it work (it will refuse to migrate the csproj files the first time around, but will import them later if you open the solution and re-import the projects. Cheers Josh, JP, Richard |