This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, we are using Lightspeed in our application to map entities to the DB. We are in the process of Unt Testing chunks of our code. One of the basic rules that we follow when we write our unit tests is: Never use a real entity (use a mock) for any object that is not being unit tested right now. So I can't use the EntityFactory.Create for any object other than the object under test. The issue I am having is that when I try to add the mocks to properties OR to the unit of work, I get a NUllException which I suspect is b/c the mock object does not have an Id. Here is an example
Entity Customer Entity Order let's say I need to test a function which internally calls customer.Orders as part of its code, so I create a bunch of Order Mocks and a real customer object: IOrder one = new Mock<IOrder>().Object; IOrder two= new Mock<IOrder>().Object; IOrder three= new Mock<IOrder>().Object; ICustomer customer = EntityFactory.Create<ICustomer>(EntityState.New, _random.Next()); customer.Orders.Add(one); - Throws Null Exception customer.Orders.Add(two); - Throws Null Exception customer.Orders.Add(three); - Throws Null Exception customer.CallSomeFunctionTHatUsesTheOrdersCollection(); - this is the function I am unit testing.
Can you please tell me if there is a way to overcome that issue. Maybe a way to give an Id to a mock?
Thanks very much in advance.
|
|
|
The object you add to the collection needs to be an entity. So you need to teach your mocking framework to create entities rather than just random objects that happen to implement IOrder. That said, a better way to deal with this might be to point out to the rules committee that CallSomeFunctionThatUsesTheOrdersCollection is, well, using the orders collection, and therefore the orders collection is part of the test, and therefore you should at least be allowed to put fakes instead of mocks. Then you can use EntityFactory to create your fake Order objects and all will be well. |
|
|
Thanks for the response Ivan. It is indeed very hard to unit test our code which highly relies on the Entity object, while trying to bypass the UnitOfWork rules. I wish there was some method as part of the EntityFactory which would create mocks that the unit of work would take. Susan
|
|