Moq Case Study and ref required

I am trying to create a test against some old method that implements parameters. Could you give me an example of how to do this?

+6
moq
source share
3 answers

Just assign the out or ref parameter from the test.

Given this interface:

 public interface ILegacy { bool Foo(out string bar); } 

You can write a test as follows:

 [TestMethod] public void Test13() { string bar = "ploeh"; var legacyStub = new Mock<ILegacy>(); legacyStub.Setup(l => l.Foo(out bar)) .Returns(true); Assert.IsTrue(legacyStub.Object.Foo(out bar)); Assert.AreEqual("ploeh", bar); } 
+19
source share

Is there something wrong with the second example at the top of http://code.google.com/p/moq/wiki/QuickStart ? You really should give examples of what you are trying to do if you are not going to look for such things.

+2
source share

By the way, if you want to use moq (currently) to mock the out parameter, you will also have to make the next hoop. Suppose you wanted to mock the out parameter, which returned another of the mocked object, for example.

 var mockServiceA = new Mock<IMyService>(); var mockServiceOutput = new Mock<IMyServiceOutput>(); // This will not work... mockServiceA.Setup(svc => svc.DoSomething(out mockServiceOutput.Object)); // To have this work you have to do the following IMyServiceOutput castOutput = mockServiceOutput.Object; mockServiceA.Setup(svc => svc.DoSomething(out castOutput)); 
+1
source share

All Articles