Assigning out/ref parameters in Moq?

Seems like it is not possible out of the box. Looks like someone attempted a solution See this forum post code.google.com/p/moq/issues/detail?id=176 this question stackoverflow.com/questions/726630/verif....

Thanks for the confirmation. I had actually found those two links in my searching, but also noticed that Moq lists one of it's features as "supporting ref/out parameters", so I wanted to be sure. – Richard Szalay Jul 1 '09 at 12:22.

For 'out', the following seems to work for me. Public interface IService { void DoIt(out string a); } TestMethod public void Test() { var service = new Mock(); var a = "output value"; service. Setup(s => s.

DoIt(out a)); string b; service.Object. DoIt(out b); Assert. AreEqual("output value", b); } I'm guessing that Moq looks at the value of 'a' when you call Setup and remembers it.

For 'ref', I'm looking for an answer also. I found the following QuickStart guide useful: http://code.google.com/p/moq/wiki/QuickStart.

1 I think the problem I had was that where is no method of assigning the out/ref params from the method Setup – Richard Szalay Jun 29 '10 at 5:45 I don't have a solution for assigning a ref parameter. This example does assign a value of "output value" to 'b'. Moq doesn't execute the Expression you pass to Setup, it analyzes it and realizes that you are providing 'a' for an output value, so it looks at the present value of 'a' and remembers it for subsequent calls.

– Parched Squid Jun 29 '10 at 15:39 See also the out and ref examples at: code.google. Com/p/moq/wiki/QuickStart – TrueWill Dec 8 '10 at 20:16 1 This won't work for me when the Mocked interface method is executed in a different scope that has its own referenced output variable (for example inside the method of another class. ) The example given above is convenient because execution occurs in the same scope as the mock setup, however it's too simple to solve all scenarios.

Support for explicit handling of the out/ref value is weak in moq (as said by somebody else, handled at execution time). – John K Mar 22 at 22:43 +1: this is a helpful answer. But: if the out parameter type is a class rather then a build-in type like string - I don't believe this will work.

Tried it today. The mock object simulates the call and returns a null via the "out" parameter. – azheglov Apr 18 at 21:15.

This can be a solution . Test public void TestForOutParameterInMoq() { //Arrange _mockParameterManager= new Mock(); Mock mockParameter= new Mock(); //Parameter affectation should be useless but is not. It's really used by Moq IParameter parameter= mockParameter.

Object; //Mock method used in UpperParameterManager _mockParameterManager. Setup(x => x. OutMethod(out parameter)); //Act with the real instance _UpperParameterManager.

UpperOutMethod(out parameter); //Assert that method used on the out parameter of inner out method are really called mockParameter. Verify(x => x. FunctionCalledInOutMethodAfterInnerOutMethod(),Times.Once()); }.

This is basically the same as Parched's answer and has the same limitation, in that it cannot change the out value depending on the input nor can it respond to ref parameters. – Richard Szalay Sep 1 at 8:53.

This is documentation from Moq site: // out arguments var outString = "ack"; // TryParse will return true, and the out argument will return "ack", lazy evaluated mock. Setup(foo => foo. TryParse("ping", out outString)).

Returns(true); // ref arguments var instance = new Bar(); // Only matches if the ref argument to the invocation is the same instance mock. Setup(foo => foo. Submit(ref instance)).

Returns(true).

This is basically the same as Parched's answer and has the same limitation, in that it cannot change the out value depending on the input nor can it respond to ref parameters. – Richard Szalay Sep 30 at 11:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions