Update of /cvsroot/nmock/nmock2/src/NMock2.AcceptanceTests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24181/src/NMock2.AcceptanceTests Added Files: .cvsignore PropertiesAcceptanceTest.cs IHelloWorld.cs ErrorCheckingAcceptanceTest.cs EventsAcceptanceTest.cs OrderedCallsAcceptanceTest.cs StubAcceptanceTest.cs AssemblyInfo.cs ExpectationsAcceptanceTest.cs IndexersAcceptanceTest.cs UsingBlockAcceptanceTest.cs Example.cs NMock2.AcceptanceTests.csproj ErrorMessageDemo.cs Log Message: first upload of nmock2 --- NEW FILE: .cvsignore --- obj NMock2.AcceptanceTests.csproj.user --- NEW FILE: Example.cs --- using NUnit.Framework; namespace NMock2.AcceptanceTests { public delegate void WhoIsThereResponse(); public delegate void WhoResponse(string firstName); public interface IKnockKnock { void KnockKnock(IJoker joker); void TellFirstName(IJoker joker, string firstName); void TellPunchline(IJoker joker, string punchline); } public interface IJoker { void Respond(string response); void Ha(); void Hee(); void Ho(); } public class Audience : IKnockKnock { public void KnockKnock(IJoker joker) { joker.Respond("Who's there?"); } public void TellFirstName(IJoker joker, string firstName) { joker.Respond(firstName + ", who?"); } public void TellPunchline(IJoker joker, string punchLine) { joker.Ha(); joker.Ha(); joker.Hee(); joker.Ho(); joker.Ho(); joker.Hee(); joker.Hee(); } } [TestFixture] public class Example { [Test] public void KnockKnockJoke() { Mockery mocks = new Mockery(); const string firstName = "Doctor"; const string punchline = "How did you know?"; IJoker joker = (IJoker)mocks.NewMock(typeof(IJoker), "joker"); Audience audience = new Audience(); using (mocks.Ordered) { Expect.Once.On(joker).Method("Respond").With(Is.EqualTo("Who's there?")); Expect.Once.On(joker).Method("Respond").With(Is.StringContaining(firstName) & Is.StringContaining("who?")); using (mocks.Unordered) { Expect.AtLeastOnce.On(joker).Method("Ha"); Expect.AtLeastOnce.On(joker).Method("Ho"); Expect.AtLeastOnce.On(joker).Method("Hee"); } } audience.KnockKnock(joker); audience.TellFirstName(joker, firstName); audience.TellPunchline(joker, punchline); mocks.VerifyAllExpectationsHaveBeenMet(); } } } --- NEW FILE: NMock2.AcceptanceTests.csproj --- <VisualStudioProject> <CSHARP ProjectType = "Local" ProductVersion = "7.10.3077" SchemaVersion = "2.0" ProjectGuid = "{B4EED566-0F75-443F-9990-FBD7E489B9BD}" > <Build> <Settings ApplicationIcon = "" AssemblyKeyContainerName = "" AssemblyName = "NMock2.AcceptanceTests" AssemblyOriginatorKeyFile = "" DefaultClientScript = "JScript" DefaultHTMLPageLayout = "Grid" DefaultTargetSchema = "IE50" DelaySign = "false" OutputType = "Library" PreBuildEvent = "" PostBuildEvent = "" RootNamespace = "NMock2.AcceptanceTests" RunPostBuildEvent = "OnBuildSuccess" StartupObject = "" > <Config Name = "Debug" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "DEBUG;TRACE" DocumentationFile = "" DebugSymbols = "true" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "false" OutputPath = "..\..\build\NMock2.AcceptanceTests\Debug\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> <Config Name = "Release" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "TRACE" DocumentationFile = "" DebugSymbols = "false" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "true" OutputPath = "..\..\build\NMock2.AcceptanceTests\Release\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> </Settings> <References> <Reference Name = "System" AssemblyName = "System" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.dll" /> <Reference Name = "System.Data" AssemblyName = "System.Data" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Data.dll" /> <Reference Name = "System.XML" AssemblyName = "System.Xml" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> <Reference Name = "nunit.framework" AssemblyName = "nunit.framework" HintPath = "..\..\lib\nunit.framework.dll" /> <Reference Name = "NMock2" Project = "{CEE959FE-C3AF-4B51-8F1A-CCB32BAA1E98}" Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" /> </References> </Build> <Files> <Include> <File RelPath = "AssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ErrorCheckingAcceptanceTest.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ErrorMessageDemo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "EventsAcceptanceTest.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Example.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ExpectationsAcceptanceTest.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IHelloWorld.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IndexersAcceptanceTest.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "OrderedCallsAcceptanceTest.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "PropertiesAcceptanceTest.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "StubAcceptanceTest.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "UsingBlockAcceptanceTest.cs" SubType = "Code" BuildAction = "Compile" /> </Include> </Files> </CSHARP> </VisualStudioProject> --- NEW FILE: ExpectationsAcceptanceTest.cs --- using NMock2.Internal; using NUnit.Framework; namespace NMock2.AcceptanceTests { [TestFixture] public class ExpectationsAcceptanceTest { [Test, ExpectedException(typeof(ExpectationException))] public void FailsTestIfMethodExpectedOnceButNotCalled() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Once.On(helloWorld).Method("Hello").WithNoArguments(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void PassesTestIfMethodExpectedOnceAndCalled() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Once.On(helloWorld).Method("Hello").WithNoArguments(); helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(ExpectationException))] public void FailsTestIfMethodExpectedOnceButCalledTwice() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Once.On(helloWorld).Method("Hello").WithNoArguments(); helloWorld.Hello(); helloWorld.Hello(); } [Test, ExpectedException(typeof(ExpectationException))] public void FailsTestIfMethodExpectedAtLeastNTimesButCalledLessThanNTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.AtLeast(N).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < N-1; i++) helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void PassesTestIfMethodExpectedAtLeastNTimesAndCalledNTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.AtLeast(N).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < N; i++) helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void PassesTestIfMethodExpectedAtLeastNTimesAndCalledMoreThanNTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.AtLeast(N).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < N+1; i++) helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void PassesTestIfMethodExpectedAtMostNTimesButCalledMoreThanNTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.AtMost(N).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < N-1; i++) helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void PassesTestIfMethodExpectedAtMostNTimesAndCalledNTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.AtMost(N).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < N; i++) helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(ExpectationException))] public void FailsTestIfMethodExpectedAtMostNTimesAndCalledMoreThanNTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.AtMost(N).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < N+1; i++) helloWorld.Hello(); } [Test, ExpectedException(typeof(ExpectationException))] public void FailsTestIfMethodExpectedBetweenNAndMTimesButCalledLessThanNTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Between(N,M).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < N-1; i++) helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(ExpectationException))] public void FailsTestIfMethodExpectedBetweenNAndMTimesAndCalledMoreThanMTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Between(N,M).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < M; i++) helloWorld.Hello(); helloWorld.Hello(); } [Test] public void PassesTestIfMethodExpectedBetweenNAndMTimesAndCalledBetweenNAndMTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Between(N,M).On(helloWorld).Method("Hello").WithNoArguments(); for (int i = 0; i < (N+M)/2; i++) helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(ExpectationException))] public void FailsTestIfMethodNeverExpectedIsCalled() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Never.On(helloWorld).Method("Hello").WithNoArguments(); helloWorld.Hello(); } [Test] public void PassesTestIfMethodNeverExpectedIsNeverCalled() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Never.On(helloWorld).Method("Hello").WithNoArguments(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(ExpectationException))] public void FailsTestIfMethodNeverExpectedIsActuallyCalled() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Never.On(helloWorld).Method("Hello").WithNoArguments(); helloWorld.Hello(); } #region Really boring stuff #region It really is boring stuff const int N = 2; const int M = 4; #endregion #endregion } } --- NEW FILE: IndexersAcceptanceTest.cs --- using System; using NMock2.Internal; using NUnit.Framework; namespace NMock2.AcceptanceTests { interface Indexed { string this[string s, string t] { get; set; } } [TestFixture] public class IndexersAcceptanceTest { [Test] public void CanExpectIndexedGetter() { Mockery mocks = new Mockery(); Indexed indexed = (Indexed) mocks.NewMock(typeof(Indexed)); Stub.On(indexed).Get["Bill","Gates"].Will(Return.Value("Microsoft")); Stub.On(indexed).Get["Steve","Jobs"].Will(Return.Value("Apple")); Assert.AreEqual("Microsoft", indexed["Bill","Gates"], "Bill, Gates"); Assert.AreEqual("Apple", indexed["Steve","Jobs"], "Steve, Jobs"); } [Test] public void CanExpectIndexedSetter() { Mockery mocks = new Mockery(); Indexed indexed = (Indexed)mocks.NewMock(typeof(Indexed)); Expect.Once.On(indexed).Set["Bill","Gates"].To("Microsoft"); indexed["Bill","Gates"] = "Microsoft"; mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void ErrorMessagesContainNameOfIndexedGetterNotHiddenMethod() { Mockery mocks = new Mockery(); Indexed indexed = (Indexed) mocks.NewMock(typeof(Indexed)); Stub.On(indexed).Get["Bill","Gates"].Will(Return.Value("Microsoft")); try { String.Intern(indexed["Steve","Jobs"]); } catch (ExpectationException e) { Assert.IsTrue(e.Message.IndexOf("get_Item") < 0, "message should not contain get_Item" ); Assert.IsTrue(e.Message.IndexOf("indexed[equal to \"Bill\", equal to \"Gates\"]") >= 0, "message should contain indexed[equal to \"Bill\", equal to \"Gates\"]\nWas: " + e.Message ); Assert.IsTrue(e.Message.IndexOf("indexed[\"Steve\", \"Jobs\"]") >= 0, "message should contain indexed[\"Steve\", \"Jobs\"]\nWas: " + e.Message ); } } [Test] public void ErrorMessagesContainNameOfPropertySetterNotHiddenMethod() { Mockery mocks = new Mockery(); Indexed indexed = (Indexed) mocks.NewMock(typeof(Indexed)); Expect.Once.On(indexed).Set["Bill","Gates"].To("Microsoft"); try { indexed["Steve","Jobs"] = "Apple"; } catch (ExpectationException e) { Assert.IsTrue(e.Message.IndexOf("set_Item") < 0, "message should not contain set_Item" ); Assert.IsTrue(e.Message.IndexOf("indexed[equal to \"Bill\", equal to \"Gates\"] = (equal to \"Microsoft\")") >= 0, "message should contain indexed[equal to \"Bill\", equal to \"Gates\"] = \"Microsoft\"\nWas: " + e.Message ); Assert.IsTrue(e.Message.IndexOf("indexed[\"Steve\", \"Jobs\"] = \"Apple\"") >= 0, "message should contain indexed[\"Steve\", \"Jobs\"] = \"Apple\"\nWas: " + e.Message ); } } } } --- NEW FILE: IHelloWorld.cs --- namespace NMock2.AcceptanceTests { public interface IHelloWorld { void Hello(); void Umm(); void Err(); void Ahh(); void Goodbye(); string Ask(string question); } } --- NEW FILE: OrderedCallsAcceptanceTest.cs --- using NMock2.Internal; using NUnit.Framework; namespace NMock2.AcceptanceTests { [TestFixture] public class OrderedCallsAcceptanceTest { private Mockery mocks; private IHelloWorld helloWorld; [SetUp] public void SetUp() { mocks = new Mockery(); helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); } [Test] public void DoesNotEnforceTheOrderOfCallsByDefault() { Expect.Once.On(helloWorld).Method("Hello"); Expect.Once.On(helloWorld).Method("Goodbye"); helloWorld.Goodbye(); helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void UnorderedExpectationsMatchInOrderOfSpecification() { Expect.Once.On(helloWorld).Method("Ask").With(Is.Anything).Will(Return.Value("1")); Expect.Once.On(helloWorld).Method("Ask").With(Is.Anything).Will(Return.Value("2")); Assert.AreEqual("1", helloWorld.Ask("ignored"), "first call"); Assert.AreEqual("2", helloWorld.Ask("ignored"), "second call"); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(ExpectationException))] public void EnforcesTheOrderOfCallsWithinAnInOrderBlock() { using (mocks.Ordered) { Expect.Once.On(helloWorld).Method("Hello"); Expect.Once.On(helloWorld).Method("Goodbye"); } helloWorld.Goodbye(); helloWorld.Hello(); } [Test] public void AllowsCallsIfCalledInSameOrderAsExpectedWithinAnInOrderBlock() { using (mocks.Ordered) { Expect.Once.On(helloWorld).Method("Hello"); Expect.Once.On(helloWorld).Method("Goodbye"); } helloWorld.Hello(); helloWorld.Goodbye(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void CanExpectUnorderedCallsWithinAnOrderedSequence() { using (mocks.Ordered) { Expect.Once.On(helloWorld).Method("Hello"); using (mocks.Unordered) { Expect.Once.On(helloWorld).Method("Umm"); Expect.Once.On(helloWorld).Method("Err"); } Expect.Once.On(helloWorld).Method("Goodbye"); } helloWorld.Hello(); helloWorld.Err(); helloWorld.Umm(); helloWorld.Goodbye(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, ExpectedException(typeof(ExpectationException))] public void UnorderedCallsWithinAnInOrderedBlockCannotBeCalledBeforeTheStartOfTheUnorderedExpectations() { using (mocks.Ordered) { Expect.Once.On(helloWorld).Method("Hello"); using (mocks.Unordered) { Expect.Once.On(helloWorld).Method("Umm"); Expect.Once.On(helloWorld).Method("Err"); } Expect.Once.On(helloWorld).Method("Goodbye"); } helloWorld.Err(); helloWorld.Hello(); helloWorld.Umm(); helloWorld.Goodbye(); } [Test, ExpectedException(typeof(ExpectationException))] public void UnorderedCallsWithinAnInOrderedBlockCannotBeCalledAfterTheEndOfTheUnorderedExpectations() { using (mocks.Ordered) { Expect.Once.On(helloWorld).Method("Hello"); using (mocks.Unordered) { Expect.Once.On(helloWorld).Method("Umm"); Expect.Once.On(helloWorld).Method("Err"); } Expect.Once.On(helloWorld).Method("Goodbye"); } helloWorld.Hello(); helloWorld.Err(); helloWorld.Goodbye(); helloWorld.Umm(); } [Test] public void CallsWithinAnInOrderedBlockCanBeExpectedMoreThanOnce() { using (mocks.Ordered) { Expect.Once.On(helloWorld).Method("Hello"); Expect.AtLeastOnce.On(helloWorld).Method("Err"); Expect.Once.On(helloWorld).Method("Goodbye"); } helloWorld.Hello(); helloWorld.Err(); helloWorld.Err(); helloWorld.Goodbye(); } } } --- NEW FILE: ErrorMessageDemo.cs --- using System; using NUnit.Framework; namespace NMock2.AcceptanceTests { [TestFixture] public class ErrorMessageDemo { public delegate void Action(); public interface ISyntacticSugar { string Property { get; set; } string this[string s] { get; set; } int this[int i,string s] { get; set; } event Action Actions; } [Test, Explicit] public void VerifyFailure() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Once.On(helloWorld).Method("Hello").WithNoArguments(); Expect.Between(2,4).On(helloWorld).Method("Ask").With("What color is the fish?") .Will(Return.Value("purple")); Expect.AtLeast(1).On(helloWorld).Method("Ask").With("How big is the fish?") .Will(Throw.Exception(new InvalidOperationException("stop asking about the fish!"))); helloWorld.Hello(); helloWorld.Ask("What color is the fish?"); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, Explicit] public void UnexpectedInvocation() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Once.On(helloWorld).Method("Hello").WithNoArguments(); Expect.Between(2,4).On(helloWorld).Method("Ask").With("What color is the fish?") .Will(Return.Value("purple")); Expect.AtLeast(1).On(helloWorld).Method("Ask").With("How big is the fish?") .Will(Throw.Exception(new InvalidOperationException("stop asking about the fish!"))); helloWorld.Hello(); helloWorld.Ask("What color is the fish?"); helloWorld.Ask("What color is the hippo?"); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test, Explicit] public void IndexerSet() { Mockery mocks = new Mockery(); ISyntacticSugar sugar = (ISyntacticSugar) mocks.NewMock(typeof(ISyntacticSugar), "sugar"); Expect.Once.On(sugar).Set[10,"goodbye"].To(12); sugar[10,"hello"] = 11; } [Test, Explicit] public void EventAdd() { Mockery mocks = new Mockery(); ISyntacticSugar sugar = (ISyntacticSugar) mocks.NewMock(typeof(ISyntacticSugar), "sugar"); Expect.Once.On(sugar).EventRemove("Actions",Is.Anything); sugar.Actions += new Action(DoAction); } private void DoAction() { throw new NotSupportedException(); } } } --- NEW FILE: StubAcceptanceTest.cs --- using NUnit.Framework; namespace NMock2.AcceptanceTests { [TestFixture] public class StubAcceptanceTest { [Test] public void StubsDoNotHaveToBeCalled() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Stub.On(helloWorld).Method("Hello").WithAnyArguments(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void StubsCanBeCalledAnyNumberOfTimes() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Stub.On(helloWorld).Method("Hello").WithAnyArguments(); for (int i = 0; i < ANY_NUMBER; i++) helloWorld.Hello(); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void StubsMatchArgumentsAndPerformActionsJustLikeAnExpectation() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Stub.On(helloWorld).Method("Ask").With("Name").Will(Return.Value("Bob")); Stub.On(helloWorld).Method("Ask").With("Age").Will(Return.Value("30")); for (int i = 0; i < ANY_NUMBER; i++) { Verify.That(helloWorld.Ask("Name"), Is.EqualTo("Bob"), "Name"); Verify.That(helloWorld.Ask("Age"), Is.EqualTo("30"), "Age"); } } #region Don't look in here... #region I told you not to look in here! const int ANY_NUMBER = 10; #endregion #endregion } } --- NEW FILE: ErrorCheckingAcceptanceTest.cs --- using System; using NUnit.Framework; namespace NMock2.AcceptanceTests { [TestFixture] public class ErrorCheckingAcceptanceTest { [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectAMethodOnAnRealObject() { object realObject = new object(); Expect.Once.On(realObject).Method(Is.Anything); } [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectAMethodThatDoesNotExistInTheMockedType() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof (IHelloWorld)); Expect.Once.On(helloWorld).Method("NonexistentMethod"); } [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectAMethodWithAnInvalidName() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof (IHelloWorld)); Expect.Once.On(helloWorld).Method("Invalid Name!"); } [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectGetOfAnInvalidProperty() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof (IHelloWorld)); Expect.Once.On(helloWorld).GetProperty("NonexistentProperty"); } [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectSetOfAnInvalidProperty() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof (IHelloWorld)); Expect.Once.On(helloWorld).SetProperty("NonexistentProperty").To("something"); } [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectGetOfIndexerIfNoIndexerInMockedType() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof (IHelloWorld)); Expect.Once.On(helloWorld).Get["arg"].Will(Return.Value("something")); } [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectSetOfIndexerIfNoIndexerInMockedType() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof (IHelloWorld)); Expect.Once.On(helloWorld).Set["arg"].To("something"); } [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectAddToNonexistentEvent() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof (IHelloWorld)); Expect.Once.On(helloWorld).EventAdd("NonexistentEvent", Is.Anything); } [Test, ExpectedException(typeof(ArgumentException))] public void CannotExpectRemoveFromNonexistentEvent() { Mockery mocks = new Mockery(); IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof (IHelloWorld)); Expect.Once.On(helloWorld).EventRemove("NonexistentEvent", Is.Anything); } } } --- NEW FILE: UsingBlockAcceptanceTest.cs --- using NMock2.Internal; using NUnit.Framework; namespace NMock2.AcceptanceTests { [TestFixture] public class UsingBlockAcceptanceTest { [Test, ExpectedException(typeof(ExpectationException))] public void AssertsExpectationsAreMetAtEndOfUsingBlock() { using(Mockery mocks = new Mockery()) { IHelloWorld helloWorld = (IHelloWorld) mocks.NewMock(typeof(IHelloWorld)); Expect.Once.On(helloWorld).Method("Hello").WithNoArguments(); } } } } --- NEW FILE: AssemblyInfo.cs --- using System.Reflection; using System.Runtime.CompilerServices; // // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. // [assembly: AssemblyTitle("")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("")] [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("1.0.*")] // // In order to sign your assembly you must specify a key to use. Refer to the // Microsoft .NET Framework documentation for more information on assembly signing. // // Use the attributes below to control which key is used for signing. // // Notes: // (*) If no key is specified, the assembly is not signed. // (*) KeyName refers to a key that has been installed in the Crypto Service // Provider (CSP) on your machine. KeyFile refers to a file which contains // a key. // (*) If the KeyFile and the KeyName values are both specified, the // following processing occurs: // (1) If the KeyName can be found in the CSP, that key is used. // (2) If the KeyName does not exist and the KeyFile does exist, the key // in the KeyFile is installed into the CSP and used. // (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. // When specifying the KeyFile, the location of the KeyFile should be // relative to the project output directory which is // %Project Directory%\obj\<configuration>. For example, if your KeyFile is // located in the project directory, you would specify the AssemblyKeyFile // attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")] // (*) Delay Signing is an advanced option - see the Microsoft .NET Framework // documentation for more information on this. // [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("")] [assembly: AssemblyKeyName("")] --- NEW FILE: PropertiesAcceptanceTest.cs --- using System; using NMock2.Internal; using NUnit.Framework; namespace NMock2.AcceptanceTests { interface PersonRecord { string FirstName { get; set; } string LastName { get; set; } } [TestFixture] public class PropertiesAcceptanceTest { [Test] public void CanExpectPropertyGetter() { Mockery mocks = new Mockery(); PersonRecord p = (PersonRecord) mocks.NewMock(typeof(PersonRecord),"p"); Stub.On(p).GetProperty("FirstName").Will(Return.Value("Fred")); Stub.On(p).GetProperty("LastName").Will(Return.Value("Bloggs")); Verify.That(p.FirstName, Is.EqualTo("Fred"), "first name"); Verify.That(p.LastName, Is.EqualTo("Bloggs"), "last name"); } [Test] public void CanExpectPropertySetter() { Mockery mocks = new Mockery(); PersonRecord p = (PersonRecord)mocks.NewMock(typeof(PersonRecord), "p"); Expect.Once.On(p).SetProperty("FirstName").To("Fred"); p.FirstName = "Fred"; mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void ErrorMessagesContainNameOfPropertyGetterNotHiddenMethod() { Mockery mocks = new Mockery(); PersonRecord p = (PersonRecord) mocks.NewMock(typeof(PersonRecord),"p"); Stub.On(p).GetProperty("FirstName").Will(Return.Value("Fred")); try { String.Intern(p.LastName); } catch (ExpectationException e) { Assert.IsTrue(e.Message.IndexOf("get_FirstName()") < 0, "message should not contain get_FirstName()\nWas: " + e.Message); Assert.IsTrue(e.Message.IndexOf("p.FirstName") >= 0, "message should contain p.FirstName\nWas: " + e.Message); Assert.IsTrue(e.Message.IndexOf("get_LastName()") < 0, "message should not contain get_LastName()\nWas: " + e.Message); Assert.IsTrue(e.Message.IndexOf("p.LastName") >= 0, "message should contain p.LastName\nWas: " + e.Message); } } [Test] public void ErrorMessagesContainNameOfPropertySetterNotHiddenMethod() { Mockery mocks = new Mockery(); PersonRecord p = (PersonRecord) mocks.NewMock(typeof(PersonRecord),"p"); Expect.Once.On(p).SetProperty("FirstName").To("Fred"); try { p.LastName = "Bloggs"; } catch (ExpectationException e) { Assert.IsTrue(e.Message.IndexOf("set_FirstName(\"Fred\")") < 0, "message should not contain set_FirstName(\"Fred\")\nWas: " + e.Message ); Assert.IsTrue(e.Message.IndexOf("p.FirstName = (equal to \"Fred\")") >= 0, "message should contain p.FirstName = \"Fred\"\nWas: " + e.Message ); Assert.IsTrue(e.Message.IndexOf("set_LastName(\"Bloggs\")") < 0, "message should not contain set_LastName(\"Bloggs\")\nWas :" + e.Message ); Assert.IsTrue(e.Message.IndexOf("p.LastName = \"Bloggs\"") >= 0, "message should contain p.LastName = \"Bloggs\"\nWas: " + e.Message ); } } } } --- NEW FILE: EventsAcceptanceTest.cs --- using NUnit.Framework; namespace NMock2.AcceptanceTests { [TestFixture] public class EventsAcceptanceTest { public delegate void Listener(string message); public interface Announcer { event Listener Listeners; } public void DummyListener(string message) { } [Test] public void CanExpectEventAdd() { Mockery mocks = new Mockery(); Announcer announcer = (Announcer) mocks.NewMock(typeof(Announcer)); Expect.Once.On(announcer).EventAdd("Listeners", new Listener(DummyListener)); announcer.Listeners += new Listener(DummyListener); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void CanExpectEventRemove() { Mockery mocks = new Mockery(); Announcer announcer = (Announcer) mocks.NewMock(typeof(Announcer)); Expect.Once.On(announcer).EventRemove("Listeners", new Listener(DummyListener)); announcer.Listeners -= new Listener(DummyListener); mocks.VerifyAllExpectationsHaveBeenMet(); } [Test] public void DelegatesCanBeComparedToEquality() { Listener l1 = new Listener(DummyListener); Listener l2 = new Listener(DummyListener); Assert.AreEqual(l1, l2); } } } |