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
0 votes
when trying to Test that Method:
internal void RefreshCollection(List<Guid> changedUids)
{
if (changedUids == null)
changedUids = new List<Guid>();

var newInstances = GetModelSourceCollection();
var newInstancesUids = newInstances.Select(e => e.GetUID()).ToList();
var sourceCollectionUids = SourceInstancesCollection.Select(e => e.GetUID()).ToList();

var collectionToRemove = PrepareCollectionToRemove(changedUids, newInstancesUids);
var collectionToAdd = PrepareCollectionToAdd(changedUids, sourceCollectionUids, newInstances);

UpdateSourceCollection(collectionToAdd, collectionToRemove);
}


The Method PrepareCollectionToAdd is that code:
internal List<TEntity> PrepareCollectionToAdd(List<Guid> changedUids, List<Guid> sourceCollectionUids, List<TEntity> newInstances)
{
var collectionToAdd = new List<TEntity>();
foreach (var newInstance in newInstances)
{
if (!sourceCollectionUids.Contains(newInstance.GetUID()))
collectionToAdd.Add(newInstance);
if (changedUids.Contains(newInstance.GetUID()) && sourceCollectionUids.Contains(newInstance.GetUID()))
collectionToAdd.Add(newInstance);
}
return collectionToAdd;
}


this is the test code:
[TestMethod, TestCategory("UnitTest")]
public void RefreshCollection_DefaultCase_PrepareCollectionToAddWasCalled()
{
// Arrange
Isolate.WhenCalled(() => _viewModel.PrepareCollectionToAdd(null, null, null)).WillReturn(CommonGenerator.GetEntities(3));
// Act

// Assert
Assert.Fail("Test not impelemnted");

}

(I've tried it alot. this is very thin example of code that failed.)

I got this Message after the test is failed:
Test method AM6.DDServer.Client.Test.Common.BaseTypes.BaseCollectionViewModelTest.RefreshCollection_DefaultCase_PrepareCollectionToAddWasCalled threw exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.


after many checks commeting the First Isolator.WhenCalled=>WillReturn will give fail on the last line.
I belive from some reason the exception is called becouse the defenition of PrepareCollectionToAdd. please let me know what is the problem.
Thanks
asked by binygal (2.8k points)

8 Answers

0 votes
Hi,

Can you please post here more data?
How do you create _viewModel? Is it a live instance or you create it with Isolate.Fake.Instance<>()?
It looks like _viewModel is a field in the test class. If you change it to be a local variable of the test, do you still get the same error?
Do you have the stack trace? Please post the full stack trace if you have it.
What is TEntity? Is it a generic parameter or a class?
answered by ohad (35.4k points)
0 votes
Hi,

Can you please post here more data?
How do you create _viewModel?
Is it a live instance or you create it with Isolate.Fake.Instance<>()?

this is the line for creating _viewModel:
_viewModel = Isolate.Fake.Instance<BaseCollectionViewModel<EntityImplement>>(Members.CallOriginal, ConstructorWillBe.Ignored);
It looks like _viewModel is a field in the test class. If you change it to be a local variable of the test, do you still get the same error?

I got the same error when changing PrepareCollectionToAdd to static method and use it like static one.

Do you have the stack trace? Please post the full stack trace if you have it.

t.a(Type A_0, Int32 A_1, Type[] A_2, Type[] A_3)
t.a(IList`1 A_0, Type[] A_1, Type[] A_2)
t.a(Object A_0, MethodBase A_1, Object[] A_2)
t.a(MethodBase A_0, Object A_1, Type A_2, Object[] A_3, Object A_4)
t.a(MethodBase A_0, Object A_1, Object[] A_2, Object A_3)
gm.a(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
AM6.DDServer.Client.Common.BaseTypes.BaseCollectionViewModel`2.PrepareCollectionToAdd(List`1 changedUids, List`1 sourceCollectionUids, List`1 newInstances) in C:WorkAm6SourceMainDDServerClientDDS.AM6.DDServer.Client.GUIDDS.AM6.DDServer.Client.CommonBaseTypesBaseCollectionViewModel.cs: line 368
AM6.DDServer.Client.Test.Common.BaseTypes.BaseCollectionViewModelTest.<RefreshCollection_DefaultCase_PrepareCollectionToAddWasCalled>b__26() in C:WorkAm6SourceMainAmadeus6AM6.DDServer.Client.Test.CommonBaseTypesBaseCollectionViewModelTest.cs: line 300
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Boolean A_5, Object[] A_6)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
AM6.DDServer.Client.Test.Common.BaseTypes.BaseCollectionViewModelTest.RefreshCollection_DefaultCase_PrepareCollectionToAddWasCalled() in C:WorkAm6SourceMainAmadeus6AM6.DDServer.Client.Test.CommonBaseTypesBaseCollectionViewModelTest.cs: line 298


What is TEntity? Is it a generic parameter or a class?

It'sgeneric parameter.
I'll be glad if you can help me with remote or something like this.
answered by binygal (2.8k points)
0 votes
Hi,

Actually thanks to the additional info you sent I reproduced the error.
It looks like a bug and I'm looking into it now to see if we can supply you with a fast fix or workaround.
answered by ohad (35.4k points)
0 votes
Hi,

I found a work around which you can use until we'll fix the bug.
Just replace the last null argument for PrepareCollectionToAdd with new List<EntityImplement>() and the test will pass
Like this:
Isolate.WhenCalled(() => _viewModel.PrepareCollectionToAdd(null, null, new List<EntityImplement>())).WillReturn(CommonGenerator.GetEntities(3)); 


Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
I've refactored all my code. this test is no longer testing my code. Thank you anyhow
answered by binygal (2.8k points)
0 votes
Same issue here. I've tried your workaround without success. Here's a code snippet to easily reproduce:

    [TestClass]
    public class TestGenericProblem
    {
        [TestMethod, Isolated]
        public void PerformTest()
        {
            // ARRANGE
            var test = new TestClass<TestGenericProblem>();
            Isolate.WhenCalled(() => test.Test(this, null, null, null)).IgnoreCall();

            // ACT
            test.Test(this, null, null, null);

            // ASSERT
            Isolate.Verify.WasCalledWithExactArguments(() => test.Test(this, null, null, null));
        }

        [TestMethod, Isolated]
        public void PerformTestWithWorkaround()
        {
            // ARRANGE
            var test = new TestClass<TestGenericProblem>();
            Isolate.WhenCalled(() => test.Test(this, null, null, new List<TestGenericProblem>())).IgnoreCall();

            // ACT
            test.Test(this, null, null, null);

            // ASSERT
            Isolate.Verify.WasCalledWithExactArguments(() => test.Test(this, null, null, null));
        }

        internal class TestClass<T>
        {
            internal void Test(T item, IList<T> list1, Queue<T> queue, List<T> list2)
            {
            }
        }
    }


Please suggest another workaround or a fix. Thank you.
answered by Christian.Merat (2.9k points)
0 votes
Hi,

This is indeed a bug, were working on a fix which will be available soon.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Hi,

We've just released a new version containing the fix to this bug. You're welcome to download it.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
...