This is a migrated thread and some comments may be shown as answers.

How to Mock an OUT variable

2 Answers 276 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
BishMan
Top achievements
Rank 1
BishMan asked on 05 Jul 2013, 05:51 PM
Hello,

I have the following code:

var mockDocumentRepository = Mock.Create<IDocumentRepository>();
  
Mock.Arrange(() => mockDocumentRepository.FetchDocumentsByListCriteria(Arg.IsAny<ListCriteria>(),  Arg.AnyInt, Arg.AnyInt, out virtualCount)).Returns(GetFakeDocumentListWithChildren_OneChildLevel());

As you can see I have an out parameter, I have not figure out how to mock virtualCount, what I did is created a variable in my test method which I do not like.   Can you provide an example of how to do that?

Also, if you can provide an example how to mock a ref parameter as well please?

Also, how can Mock a delegate that I pass into method under test, right now I create a method in my test class that matches the delegate signature and pass that across, is there a better option?

I am using 2013.2.701.0 version.

2 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 08 Jul 2013, 12:06 PM
Hello Jay,

Thank you for contacting Telerik support.

At this point JustMock does not provide matchers for a ref/out parameters. This means, the only way to arrange a method using such parameters is to use predefined variables. Exactly the approach you are using. However, this should work for both out and ref parameters as shown below:
    [TestMethod]
    public void ShouldArrangeMethodWithRefAndOutParams()
    {
        var virtualCount = new VirtualCount();
 
        var mockDocumentRepository = Mock.Create<IDocumentRepository>();
 
        Mock.Arrange(() => mockDocumentRepository.FetchDocumentsByListCriteria(
            Arg.IsAny<ListCriteria>(), Arg.AnyInt, Arg.AnyInt, out virtualCount, ref virtualCount))
                .Returns(GetFakeDocumentListWithChildren_OneChildLevel());
 
        var virtualCount2 = new VirtualCount();
        var actual = mockDocumentRepository.FetchDocumentsByListCriteria(
            new ListCriteria(), 2, 3, out virtualCount2, ref virtualCount2);
 
        Assert.AreEqual(1, actual.Count);
    }
     
}
 
interface IDocumentRepository
{
    List<string> FetchDocumentsByListCriteria(
        ListCriteria listCrit, int i1, int i2, out VirtualCount virCount, ref VirtualCount virCountRef);
}

We have the implementation of such matchers in our backlog for future implementation. Please, let me know if you wish to be notified when the feature is included in an official JustMock release.

About your last question, check the next example and see if it works for you:
    [TestMethod]
    public void ShouldPassDelegateViaMatcher()
    {
        var myMock = Mock.Create<DelegateTest>();
        var called = false;
 
        Mock.Arrange(() => myMock.CallDelegate(Arg.IsAny<MyDelegate>()))
            .DoInstead(() => called = true);
 
        myMock.CallDelegate(new MyDelegate(SomeMethod));
 
        Assert.IsTrue(called);
    }
     
}
public delegate void MyDelegate();
 
class DelegateTest
{
    public void CallDelegate(MyDelegate d)
    {
        throw new NotImplementedException();
    }
}

Let me know if there is anything else, I could help you with.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Kaloyan
Telerik team
answered on 19 Aug 2013, 02:57 PM
Hello Jay,

We have included the ref matchers in our latest JustMock official release (2013.3.819 IB).

You can navigate to this link and download the installer (.msi) or install it from the Telerik Control Panel as an Internal Build.

Further, you can check our online help article for examples and detailed explanation of the feature.

I hope this helps.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
General Discussions
Asked by
BishMan
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or