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
Hi,
I'm testing a class which has a method that uses a DevExpress.XPCollection.
Since the collection is created inside the method, I mocked it using

XPCollection<OfertaActiva> xpCol = Isolate.Fake.Instance<XPCollection<OfertaActiva>>(Members.CallOriginal);
Isolate.Swap<XPCollection<OfertaActiva>>().With(xpCol);


But just after the creation of the collection there is a foreach iteration, and there begins my nightmare. It seems to me that I successfully mocked the collection, but once it tries to iterate the collection it throws an exception.
DevExpress.Xpo.XPBaseCollection.BeginLoad()
DevExpress.Xpo.XPBaseCollection.Load()
DevExpress.Xpo.XPBaseCollection.get_Objects()
DevExpress.Xpo.XPBaseCollection.System.Collections.IEnumerable.GetEnumerator()
d__0.MoveNext()
GestionNet.Singleton.GestorPrecioVenta.ObtenerPrecioConOfertas(Boolean isCongelada, Boolean isReabierta, Cliente cliente, Articulo articulo, Double unidadesCaja, DateTime fecha, Double cantidad, Boolean isCajas, Int32& tipoPrecioAplicado, Decimal& precio, Double& pcDescuento)


I added items to xpCol (the faked collection) using xpCol.Add(obj).

Any hint?

Thanks in advance.
asked by rsaladrigas (1.8k points)

8 Answers

0 votes
Doh!
Since the constructor is mocked, the collection is not initialized. I guess this is why the iterator throws an exception. So the question now turns into: How can I swap a future instance of XPCollection with my own?
The collection is instantiated inside a method of the class I'm testing.

Thanks in advance,
answered by rsaladrigas (1.8k points)
0 votes
Hi rsaladrigas,

I just wanted to let you know I'll be looking into this and will try to recreate this behavior here. There seems to be something wrong with the way things have been initialized when we created the fake object. I'll have to get back to you on that.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi Roger.

This is indeed a bug :oops: . We've managed to reproduce it here.

A question: You are using a real future object for XPCollection, but you haven't posted any WhenCalled or Verify on its fake. If there are no usages, why do you need to fake it?

Can you post the entire test and code-under-test? maybe we can write the test differently, and make it work.

Thanks
answered by gilz (14.5k points)
0 votes
Hi Gilz,

The class I'm testing uses an XPCollection on one of its methods to load data from the database. More specifically it loads a "product offers" table, since I don't want to setup a database for tests, I need to swap this collection with one of my own (which I need to initialize with proper values).

The code I'm using looks something like this (but I add a few more OfertaActiva objects to the collection).

OfertaActiva oferta = new OfertaActiva(Session.DefaultSession);
XPCollection<OfertaActiva> xpCol = Isolate.Fake.Instance<XPCollection<OfertaActiva>>(Members.CallOriginal);
Isolate.WhenCalled(() => xpOfertas.Session).WillReturn(Session.DefaultSession);
Isolate.WhenCalled(() => xpOfertas.Add(oferta)).CallOriginal();
Isolate.Swap<XPCollection<OfertaActiva>>().With(xpCol); 
//setup *oferta* with the desired values
xpCol.Add(oferta);


Hope this helps to show you where I'm trying to go (even if I'm going the wrong way :oops:)
answered by rsaladrigas (1.8k points)
0 votes
Hi Roger,

Yes, I see. You're trying to to do a halfway container- works like a container, but returns fake values.

I'm sorry I don't have a quick solution for you right now. But I will, and then I'll get back to you regarding a solution.

Thanks,
answered by gilz (14.5k points)
0 votes
Hi,
No good news on this issue?

Thanks,
answered by rsaladrigas (1.8k points)
0 votes
Hi Roger,

Actually there are! (and I apologize for not returning to you then). We've just released Isolator 5.1.1 which allows you to swap collections - which is what you need.

You can read some more about swapping collections and download the new version from the download page.

Please try it and let me know if it works.
answered by gilz (14.5k points)
0 votes
Hi Gil,
After reading the post about Swapping collections I managed to solve the problem. It works exactly as I expected.

Thank you very much!

This is the code used (for further reference)
OfertaActiva oferta = new OfertaActiva();
//Set the desired properties for  oferta.

XPCollection<OfertaActiva> MyCol = Isolate.Fake.Instance<XPCollection<OfertaActiva>>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(() => MyCol).WillReturnCollectionValuesOf(new List<OfertaActiva> { oferta });
Isolate.Swap.NextInstance<XPCollection<OfertaActiva>>().With(MyCol);
answered by rsaladrigas (1.8k points)
...