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
Hello...

I have a problem. I cannot mock my method.

public class MyClass
{
    public static List<string> GetStringList()
    {
        List<string> stringList = new List<string>();

        ClassToMock classToMock = new ClassToMock();
        classToMock.FillStringList(stringList);

        return stringList;
    }
}


And my test...

[Test]
public void GetNewsList()
{
    MockManager.Init();

    Mock classToMock = MockManager.Mock(typeof (ClassToMock));
    classToMock.ExpectCall("FillStringList").Args(GetFilledStringList());
            
    List<string> stringList = MyClass.GetStringList();

    Assert.AreEqual(2, stringList.Count);
    Assert.AreEqual("Mocked one!", stringList[0]);
    Assert.AreEqual("Mocked two!", stringList[1]);
            
    MockManager.Verify();
}

private static List<INews> GetFilledNewsList()
{
    List<string> stringList = new List<string>();
    stringList.Add("Mocked one!");
    stringList.Add("Mocked two!");

    return stringList;
}


I know that my call "classToMock.ExpectCall("FillStringList").Args(GetFilledStringList());" is false, but I don't know what I must do.
The method "FillStringList" should be filled the List!
asked by time-master (640 points)

3 Answers

0 votes
Hi
It seems like you have a typing error in your code.
You filled the list with GetFilledStringList()
When actually it should be GetFilledNewsList().
answered by ohad (35.4k points)
0 votes
Hi,
Welcome to the forum :-)

:arrow: It seems that you want to mock the FillStringList() and instead of calling the real code, you want to mock it, but you want to perform some action on the argument passed and actually fill the List with fake data.

:idea: The Args is used to verify that the arguments passed are expected.
GetFilledNewsList() will be called before the mock is created and then the list passed to FillStringList will be validated to be the same value as the list created in GetFilledNewsList(), this is not true and your test will fail.

:arrow: There are a few ways that you can perform actions on an argument.
The most used are DynamicReturnValue and Custom Argument Checkers

Here is an example:

[Test]
public void GetNewsList()
{
    MockManager.Init();

    Mock classToMock = MockManager.Mock(typeof (ClassToMock));
    // use dynamic return on a void method
    classToMock.ExpectAndReturn("FillStringList",new DynamicReturnValue(GetFilledNewsList));
           
    List<string> stringList = MyClass.GetStringList();

    Assert.AreEqual(2, stringList.Count);
    Assert.AreEqual("Mocked one!", stringList[0]);
    Assert.AreEqual("Mocked two!", stringList[1]);
           
    MockManager.Verify();
}

private static object GetFilledNewsList(object[] parameters, object context)
{
    // get first parameter
    List<string> stringList = (List<string>)parameters[0];
    stringList.Add("Mocked one!");
    stringList.Add("Mocked two!");

    return null; // void method
} 
answered by scott (32k points)
0 votes
Thank you! My test is running now!!!
answered by time-master (640 points)
...