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
|
I am trying to use TestUnitOfWork to isolate Lightspeed from having to hit a database in my unit tests.
var user1 = EntityFactory.Create<User>(EntityState.Default); IUnitOfWork uow = new TestUnitOfWork(); uow.SetExpectedCollectionResults(expectedUsers); var test = uow.Query<User>().ToList();
I expect by now that the test variable should hold a list of 3 users. Instead, it fails miserably with the following: System.NullReferenceException : Object reference not set to an instance of an object.
Is this a bug with Lightspeed or am I doing something wrong? I've tried to find some examples of how to use the TestUnitOfWork class both on Google, in the documentation and the samples provided, the test methods in the samples however don't ever use this class. The sample unit tests never use this class, which i find rather weird, as its sole purpose is to use it in unit tests... Could you kindly provide some proper samples on how to use this class?
Greetings, Safurudin
|
|
|
The problem is in this line: var expectedUsers = new List<User> { user1, user2, user3 } as IList<Entity>; This will always set expectedUsers to null, because C# lists are not covariant: an IList<User> is NOT an IList<Entity>. (Try replacing the "as" with a cast and you'll see the exception.) Change the line to the following: var expectedUsers = new List<Entity> { user1, user2, user3 }; |
|
|
Did as you suggestion Ivan, but am still getting the Nullreference, here is the revised code: _unitOfWork = new TestUnitOfWork(); |
|
|
Oh nevermind, I forgot to setup the SetExpectedCollectionResult of course :) Thanks for the feedback Ivan |
|
|
OK - I've discovered a new bug The following: _unitOfWork = new TestUnitOfWork(); System.NullReferenceException : Object reference not set to an instance of an object.
at Mindscape.LightSpeed.Linq.Sqo.CriteriaSqo.ExtractBinaryCriteria(LinqQueryPlanExpression plan, BinaryExpression expression)
at Mindscape.LightSpeed.Linq.Sqo.CriteriaSqo.ExtractCriteria(LinqQueryPlanExpression plan, Expression expression) at Mindscape.LightSpeed.Linq.Sqo.CriteriaSqo.ExtractCriteria(LinqQueryPlanExpression plan, Expression expression) at Mindscape.LightSpeed.Linq.Sqo.CriteriaSqo.ExtractCriteria(LinqQueryPlanExpression plan, Expression expression) at Mindscape.LightSpeed.Linq.Sqo.Where.ExtractWhereCriteria(MethodCallExpression expression, LinqQueryPlanExpression plan, GroupResultsPlan groupPlan) at Mindscape.LightSpeed.Linq.Sqo.Where.Evaluate(ExpressionVisitor visitor, MethodCallExpression expression) at Mindscape.LightSpeed.Linq.Plan.LinqQueryBuilder.VisitMethodCall(MethodCallExpression exp) at Mindscape.LightSpeed.Linq.Sqo.First.Evaluate(ExpressionVisitor visitor, MethodCallExpression expression) at Mindscape.LightSpeed.Linq.Plan.LinqQueryBuilder.VisitMethodCall(MethodCallExpression exp) at Mindscape.LightSpeed.Linq.LinqQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) at System.Linq.Queryable.First(IQueryable`1 source) It seems as putting any Linq Extension methods as Where or First on the Query for the testunitof work fails. Any solution for this?
|
|
|
LINQ needs to know which database you think you're connecting to (because CLR expressions translate to operator or function names differently on the different databases). Therefore, the unit of work needs to be associated with a LightSpeedContext. Normally, this isn't an issue because the UOW is created from a LightSpeedContext so it's already set up. However, that doesn't happen with the TestUnitOfWork. The easiest way around this is just to set the TestUnitOfWork.Context property: _unitOfWork = new TestUnitOfWork { Context = new LightSpeedContext() }; It should be okay to use an empty context because the TestUnitOfWork never actually issues a query, so it doesn't matter if the LINQ provider chooses the 'wrong' translations for your real database. We should probably make TestUnitOfWork automatically assign itself a dummy context so you don't run up against this, but this will solve your problem for now. Thanks for drawing our attention to this! |
|
|
OK - will try this. As i mentioned earlier, to demonstrate proper use, this would probably be a nice part of the samples package. Also, do you have documented some kind of "good practices" with regards to using the contexts, unitofwork and the various methods and overloads - tradeoffs and benefits... if not, this could perhaps be a nice series of blog posts - just a tip :) Thanks for the feedback |
|