December 2007 Entries

Moq, doing mock objects right?

Reading Scott Hanselman's blog I came across a link to Moq, which is a mock object that uses the new C# language features in order to do it's goodness. It looks like a pretty amazingly easy to use mock framework for your unit tests that allows you to set return values from your mock objects by setting conditions through Linq. For example (shamelessly taken from their examples):// ShouldExpectCallWithArgumentvar mock = new Mock<IFoo>();mock.Expect(x => x.DoInt(1)).Returns(11);mock.Expect(x => x.DoInt(2)).Returns(22);Assert.AreEqual(11, mock.Instance.DoInt(1));Assert.AreEqual(22, mock.Instance.DoInt(2)); Which is all that is needed to create a function that returns specific values based on the input, and there's a lot more...