West Wind Toolkit for .NET
Unit Testing with WestWind.Data
Gravatar is a globally recognized avatar based on your email address. Unit Testing with WestWind.Data
  Joel Leach
  All
  Jan 17, 2017 @ 02:27pm

Hi Rick,

I'm using WestWind.Data business objects in my current project (.NET 4.6 & EF6, not the new Core versions). I am writing unit tests for the business objects using JustMock. It is all working well, except for the Delete() and DeleteInternal() methods. Those contain "non-generic" DbSet parameters, as you can see here and here. The delete works fine in production, but in a mocked environment, I get the following error:

"Conversion between generic and non-generic DbSet objects is not supported for test doubles."

This appears to be a hard limitation in EF6. Is there any reason that the DbSet parameter needs to be non-generic? Changing the parameter type from DbSet to DbSet< TEntity > solves the problem for me, but that requires me to make a complete copy of the Delete() and DeleteInternal() methods. Is that something you could change on your end without breaking existing code?

I also ran into a problem with this line of code, because GetEntityEntry() returns null in a mocked environment. I'm working around that issue by mocking GetEntityEntry(), but you may want to do a null check before setting the State property.

Thanks,

Joel

Gravatar is a globally recognized avatar based on your email address. re: Unit Testing with WestWind.Data
  Rick Strahl
  Joel Leach
  Jan 17, 2017 @ 05:47pm

Hi Joel,

These versions are non-generic because they don't need to be generic meaning the more general version is always preferred.

The reason the parameter exists at all is to allow you to pass in a different DbSet for a delete operation against the non specified table. This happens if you have logical entities that are not mapped to top level business objects (like lineitems for example which are part of an order bus object).

DbSet<T> wouldn't make sense as a parm since the business object has that as an internal member. We already know what that is without having to pass a parameter.

The parm also allows a wwBusines instance to not have to be tied to the business objects DbSet so if you have general version of a business object you can actually pass in a DbSet. If this wasn't the case the parameter would be completely superfluous since the bus object already has the DbSet<T> it can access.

Not sure what the solution is for this. I think the best way to handle this might be to add a custom delete method with a different name that matches the signature you want.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Unit Testing with WestWind.Data
  Joel Leach
  Rick Strahl
  Jan 18, 2017 @ 07:28am

Hi Rick,

That makes sense. I essentially did what you recommend by copying the Delete() and DeleteInternal() methods up to my subclass and changing the method signature.

By the way... we are eventually going to move to ASP.NET Core and EF Core. Do you have plans to update these classes? I'm not too deep into this project yet, and as I get my feet wet with TDD, I'm considering multiple approaches. I'm currently using NuGet to get WestWind.Data and customizing things in a subclass. Testability has been fairly good, except for the minor caveats mentioned in this thread. I could also pull your source code into my project directly and modify it as needed. Or I could take a non-inheritance approach and create a generic repository. "Inheritance" seems to be a bad word these days. Of course, as an old Fox dev, I don't agree with that, but I can see how a more generic approach could improve testability. That then raises the question of how to deal with things like hooks and validations. One reason I chose your stuff was because of its simplicity compared to other solutions out there, and I don't want to lose that. As I contemplate things, it would be good to know what your thoughts/plans are moving forward.

One more thing... I'm loving the new message board, but is there a way to get email notifications of thread replies?

Thanks,

Joel

Gravatar is a globally recognized avatar based on your email address. re: Unit Testing with WestWind.Data
  Rick Strahl
  Joel Leach
  Jan 18, 2017 @ 01:02pm

Joel,

Yes eventually I will move this stuff to .NET Core, but I've stopped adapting anything for now because of the churn for .NET Standard 2.0 and the new base runtimes. There's no good way to be on that code base at the moment without constant breakage and I'm not in the mood for dealing with that right now.

The first cut of what this will look like is provided in this project:

The repository implementation is basically a slightly slimmed down version of the Westwind.BusinessObjects class.

I don't think there will be side by side versions - the differences in the EF versions are too significant to try and build an exact replica. But we'll see how that shakes out once the .NET Standard 2.0 specs finalize and what EF Core does with that.

As to emails - they are getting sent. Make sure you add support at west-wind.com to your trusted senders or check your SPAM folders and mark as not junk.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Unit Testing with WestWind.Data
  Joel Leach
  Rick Strahl
  Jan 18, 2017 @ 01:44pm

That plan sounds good. I got the email notification this time. Not sure what happened on the previous reply, but it is working.

Thanks,

Joel

© 1996-2024