Out and Ref parameters with FakeItEasy
Asked Answered
K

1

28

I have a method that has an out parameter that returns a number of records. I would like to know how to mock it with FakeItEasy.

Koger answered 30/3, 2011 at 21:34 Comment(0)
D
51

You should use the .AssignsOutAndRefParameters configuration method:

[Test]
public void Output_and_reference_parameters_can_be_configured()
{
    var fake = A.Fake<IDictionary<string, string>>();
    string ignored = null;

    A.CallTo(() => fake.TryGetValue("test", out ignored))
        .Returns(true)
        .AssignsOutAndRefParameters("foo");

    // This would of course be within you SUT.
    string outputValue = null;
    fake.TryGetValue("test", out outputValue);

    Assert.That(outputValue, Is.EqualTo("foo"));
}
Donau answered 31/3, 2011 at 7:7 Comment(3)
@Patrik Hägne, If i have 2 out arguments in a function then how pass it to AssignsOutAndRefParameters() function.Flashover
@UmeshaMS, `.AssignsOutAndRefParameters("out1", "out2")Jehoshaphat
Link to the official documentation: fakeiteasy.readthedocs.io/en/stable/…Degrease

© 2022 - 2024 — McMap. All rights reserved.