Skip to content

Loading…

How can I mock a function with ref parameter? #105

Open
danicomas opened this Issue · 4 comments

3 participants

@danicomas

I have this simple code, but I can't do it work

[TestMethod]
        public void TestMoq()
        {
            var mock = new Moq.Mock<Hola>();            

            string o = "1";
            mock.Setup(x => x.DoSomething(ref o)).Callback((string a) => o = "2");

            mock.Object.DoSomething(ref o);

        }
public class Hola
    {
        public Hola()
        {

        }

        public virtual void DoSomething(ref string a)
        {

        }
    }

The error is this:
capturemoqbug

I prefer to use the Moq library. But only I get it on the Rhino Mocks library, with the function OutRef:

[TestMethod]
        public void TestRhinoMocks()
        {
            MockRepository mocker = new MockRepository();

            Hola mock = mocker.StrictMock<Hola>();

            string o = "1";

            Expect.Call(() => mock.DoSomething(ref o)).OutRef("2");

            mocker.ReplayAll();

            //i.Setup(x => x.DoSomething(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback((string a, string b, string c) => o = "2");

            mock.DoSomething(ref o);
        }

Thanks in advance!

@kzu
Moq Team member
kzu commented

Questions should be directed to moqdisc@googlegroups.com

@danicomas

Yes @kzu , but is possible or not? If is not possible it is an issue.

@kzu
Moq Team member

You can mock, yes. You can't capture the value via a callback though.

@jinfu-kooboo

@kzu but how? It throws an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.