Return to Snippet

Revision: 14419
at June 3, 2009 18:08 by rengber


Initial Code
public Sandbox GetMockedSandbox(string helloPrefix, string ackWord, int factor)
        {
            Sandbox retVal = new Sandbox();

            var mockFactory = new Mock<IWorkerFactory>();
            var mockWorker = new Mock<IWorker>();
            
            mockFactory.Expect(fac => fac.GetWorker(It.IsAny<int>())).Returns(mockWorker.Object);

            mockWorker.Expect(ds => ds.Hello(It.IsAny<string>())).Returns((string s) => helloPrefix + s);
            mockWorker.Expect(ds => ds.Ping()).Returns(ackWord);
            mockWorker.Expect(ds => ds.MultiplyYourNumber(It.IsAny<int>())).Returns((int i) => i * factor); 

            retVal.ImplementationFactory = mockFactory.Object;
            return retVal; 
        }

Initial URL
http://code.google.com/p/moq/wiki/QuickStart

Initial Description
Note that the interface member is mockVar.Object mockVar won't cast to your interface.

Initial Title
Using Moq to Return a Mock Object from a Factory

Initial Tags

                                

Initial Language
C#