chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

I'm having trouble stubbing return values for 'out' parameters in my test class, following the instructions on the 'Learn' page for returning values for 'out' parameters from a method call on a mocked instance (the examples on the Learn page actually apply to a static rather than instance method, but I'm assuming the syntax shouldn't be any different). For example:

[TestFixture]
public class TypeMockTest
{
    [Test]
    public void Test()
    {
        //set up expectations
        OutParameter stubReturnValue = new OutParameter("2");
        OutParameter supplied = null ;
        Mock stubbedClassMock = MockManager.Mock(typeof(StubbedClass));
        stubbedClassMock.ExpectAndReturn("StubbedMethod", stubReturnValue).Args("ignored", new Assign(supplied));
        //perform the test
        ClassUnderTest classUnderTest = new ClassUnderTest();
        string actual = classUnderTest.Call(new StubbedClass());
        Assert.AreEqual(stubReturnValue.value + "1", actual);
    }
}

public class ClassUnderTest
{
    public string Call(StubbedClass ctlr)
    {
        OutParameter returnValue;
        ctlr.StubbedMethod("ignored", out returnValue);
        return returnValue.value + "1";
    }
}

public class StubbedClass
{

    public void StubbedMethod(string param1, out OutParameter outparam)
    {
        outparam = new OutParameter("2");
    }
}

public class OutParameter
{
    public string value;
    public OutParameter(string value)
    {
        this.value = value;
    }
}


I would expect the StubbedMethod to return the supplied stubReturnValue and for my test to pass, but instead I get:

TestCase 'TestSolution.TypeMockTest.Test'
failed: TypeMock.TypeMockException : 
*** No method StubbedMethod in type TestSolution.StubbedClass returns TestSolution.OutParameter
   at TypeMock.Mock.a(String A_0, Object A_1, Boolean A_2, Boolean A_3, Int32 A_4, Type[] A_5)
   at TypeMock.Mock.ExpectAndReturn(String method, Object ret, Int32 timesToRun, Type[] genericTypes)
   at TypeMock.Mock.ExpectAndReturn(String method, Object ret, Type[] genericTypes)
   D:TestSolutionTypeMockTests.cs(56,0): at TestSolution.TypeMockTest.Test()


What am I doing wrong? I'm using the latest version of Typemock, 4.2.3.

Thanks,
Nick
asked by zcrar70 (2.9k points)

3 Answers

0 votes
Hi
You've got problems here:
1. StubbedMethod returns void so you should use
stubbedClassMock.ExpectCall

instead of
stubbedClassMock.ExpectAndReturn

This the meaning of the exception you got from Typemock
"No method StubbedMethod in type StubbedClass returns OutParameter"


2. You are are assigning the variable 'supplied' which is initiated to null value. if you'll replace it to 'stubReturnValue' than the test will work as expected. 8)
Try this:
[Test]
public void Test()
{
   //set up expectations
   OutParameter stubReturnValue = new OutParameter("2");
   Mock stubbedClassMock = MockManager.Mock(typeof (StubbedClass));
   stubbedClassMock.ExpectCall("StubbedMethod").Args("ignored", new Assign(stubReturnValue));
   
   //perform the test
   ClassUnderTest classUnderTest = new ClassUnderTest();
   string actual = classUnderTest.Call(new StubbedClass());
   Assert.AreEqual(stubReturnValue.value + "1", actual);
}


Conclusion:
:arrow: When you want to mock a method that returns a value use:
Mock.ExpectAndReturn
When you want to mock a method that returns void use:
Mock.ExpectCall
:arrow: When you are using:
new Assign(someValue) Typemock Isolator will replace the parameter in the actual call with what you assigned .
Simple example:
int someValue = 3;
int x;
mock.ExpectCall("SomeMethod").Args(new Assign(someValue));
someObject.SomeMethod(out x)
//now x = 3
answered by ohad (35.5k points)
0 votes
Hi Ohad,

thanks a lot - that makes sense, and does indeed work as expected. It's easy when you know!

BTW, the example in the "Returning values of out and ref arguments" section in https://www.typemock.com/Docs/HowTo.html is a little unclear; your example explains it much better. Perhaps it could be included on the page?

[Test]
[VerifyMocks]
public void TestWithReflective()
{
  Mock mock = MockManager.Mock(typeof(ClassToIsolate));
  mock.ExpectAndReturn("StaticMethod", "fake-return-value").Args(new Assign("fake-out-value"));

  string outValue = "supplied-out-value";
  ClassToIsolate.StaticMethod(outValue);
  Assert.AreEqual("fake-out-value", outValue); //will succeed
}

Thanks,
Nick
answered by zcrar70 (2.9k points)
0 votes
Hi
Glad I could help. 8)
Thanks for your feedback on the Howto's.
We will fix it to make it clearer to the users.
answered by ohad (35.5k points)
...