This is a migrated thread and some comments may be shown as answers.

Sequential mocking (ReturnsMany) does not work

4 Answers 133 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Stanislav
Top achievements
Rank 1
Stanislav asked on 05 Dec 2016, 09:25 AM

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)

 

4 Answers, 1 is accepted

Sort by
0
Stanislav
Top achievements
Rank 1
answered on 05 Dec 2016, 10:05 AM

Forgot to mention the version of JustMock:

2014.2.811.1

0
Svetlozar
Telerik team
answered on 08 Dec 2016, 09:46 AM
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
0
Stanislav
Top achievements
Rank 1
answered on 08 Dec 2016, 02:00 PM

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.

 

0
Svetlozar
Telerik team
answered on 08 Dec 2016, 02:50 PM
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
Tags
General Discussions
Asked by
Stanislav
Top achievements
Rank 1
Answers by
Stanislav
Top achievements
Rank 1
Svetlozar
Telerik team
Share this question
or