Sequential mocking (ReturnsMany) does not work

5 posts, 0 answers
  1. Stanislav
    Stanislav avatar
    3 posts
    Member since:
    Jan 2016

    Posted 05 Dec 2016 Link to this post

    For some reason, the ReturnsMany method does not work for me. I have the follwing code:

    List<ORGANIZATION> existingOrganizations = new List<ORGANIZATION>()
                {
                    new ORGANIZATION() {ID = 1, NAME = "Organization1", ISINCLUDEDINSAMPLE = false},
                    new ORGANIZATION() {ID = 2, NAME = "Organization2", ISINCLUDEDINSAMPLE = false},
                    new ORGANIZATION() {ID = 3, NAME = "Organization3", ISINCLUDEDINSAMPLE = false},
                };

     

    container.Arrange<IOrganizationRepository>(or => or.GetById(Arg.AnyLong)).ReturnsMany(existingOrganizations);

     

    And the exception I'm getting:

    System.InvalidCastException : Unable to cast object of type 'System.Collections.Generic.List`1[Cpims.WFM.Domain.Organizations.ORGANIZATION]' to type 'Cpims.WFM.Domain.Organizations.ORGANIZATION'.
       at Castle.Proxies.IOrganizationRepositoryProxy.GetById(Int64 primaryKey, Expression`1[] includeNavigationProperties)

     

  2. Stanislav
    Stanislav avatar
    3 posts
    Member since:
    Jan 2016

    Posted 05 Dec 2016 Link to this post

    Forgot to mention the version of JustMock:

    2014.2.811.1

  3. Svetlozar
    Admin
    Svetlozar avatar
    294 posts

    Posted 08 Dec 2016 Link to this post

    Hello,

    Thank you for your question.

    Looking at your test I assume that GetById should return an IEnumerable or some collection type as you arrange it to return many.

    I created a sample project and it is working on my side. On the other hand I am not really sure whether GetById should return an object of type Organization. If that is the case you should arrange GetById to return object, not a collection.

    I guess we are missing the big picture here. Could you please send us a complete sample project that we can work with?

    Regards,
    Svetlozar
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  4. Stanislav
    Stanislav avatar
    3 posts
    Member since:
    Jan 2016

    Posted 08 Dec 2016 in reply to Svetlozar Link to this post

    Hi

    Your assumptions are wrong. The GetById method is supposed to return a single Organization entity. And I want this entity to come from my existingOrganizations list. This is why I'm arranging it with ReturnsMany() and not with Returns().

    Taken form JustMock documentation:

    "The ReturnsMany method will arrange certain function/property to sequentially return the members of a predefined array (returnValues)."

     

    So my expectation is that every time I hit GetById() in my test, I will get a different entity from the list. But this is not happening, because the whole list gets returned instead.

     

  5. Svetlozar
    Admin
    Svetlozar avatar
    294 posts

    Posted 08 Dec 2016 Link to this post

    Hello,

    I am really sorry for the misunderstanding. 

    I managed to reproduce the issue. existingOrganizations should be an array as you are using the params overload of ReturnsMany.

    Regards,
    Svetlozar
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top