1

Hoping someone can help me.

I am trying to test a method (Outer Method) that has as a dependency a class that invokes another method (Inner Method). This inner method takes a Boolean as a Ref Parameter, and my problem is that I am so far been unable to control this Boolean Ref parameter.

(Note-The code shown below has been written for the purpose of illustrating the problem and is not what the code really looks like).

The MOQ documentation from here - https://github.com/Moq/moq4/wiki/Quickstart gives an overview of dealing with Ref/Out parms but I have not found it that helpful.

I tried an example that works (which I found here - Assigning out/ref parameters in Moq)

public interface IService
{
    void DoSomething(out string a);
}

[Test]
public void Test()
{
    var service = new Mock<IService>();
    var expectedValue = "value";
    service.Setup(s => s.DoSomething(out expectedValue));

    string actualValue;
    service.Object.DoSomething(out actualValue);
    Assert.AreEqual(actualValue, expectedValue);
}

But when I tried the code that I actually want to run I cannot get it to work. This is included below.

The interface

Public Interface IGetValue
    Function GetValue(ByRef myBoolOfInterest As Boolean) As Single
End Interface

The code I wish to test

Public Class ClassToBeTested

    Dim isProblem As Boolean

    Public Function PassComparisonValues(m_GetValueGetter As IGetValue) As    Boolean

        Dim dPBar As Single = m_GetValueGetter.GetValue(isProblem)

        Return isProblem
    End Function
End Class

The code that I have written to test this is below (Note - this is a different project).

public void MethodToTest()
{
     // Arrange
     // System Under Test
     ClassToBeTested myClassToBeTested = new ClassToBeTested();

     // Construct required Mock
     Mock<IGetValue> myMock = new Mock<IGetValue>();
     bool isProblem = true;
     myMock.Setup(t => t.GetValue(ref isProblem));

     // Act
     isProblem = myClassToBeTested.PassComparisonValues(myMock.Object);

     // Assert
     Assert.That(isProblem, Is.EqualTo(true));
}

What I want is to be able to control the contents of isProblem in ClassToBeTested, and i am finding that this is not happening. It contains false no matter what I do.

Hope someone can help.

Community
  • 1
  • 1
Paul
  • 101
  • 5

1 Answers1

0

What you could do is to make isProblem protected in the ClassToBeTested. Then just for the unit tests inherit from ClassToBeTested and set the value of isProblem to expected value.

class ClassToBeTested
{
    protected bool isProblem;
    // code here ...
}

class TestableClassToBeTested 
    : ClassToBeTested
{
    public TestableClassToBeTested(bool isPRoblemValue)
    {
        isProblem = isPRoblemValue;
    }
}

public void MethodToTest()
{
    // Arrange
    // System Under Test
    bool isProblem = true;
    ClassToBeTested myClassToBeTested = new TestableClassToBeTested(isProblem);

    // code here ...
}
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51