This section updated the DAL test project to use the new StoreContext constructor and makes sure all of the tests pass for the DAL.
- Now, it's time to migrate the test for the Data Access Layer and make sure everything works. So, let's copy the context tests and the shopping cart repo tests into the DAL test project. And, for the the context test class no changes are needed. The shopping cart repo tests require a little bit of a change and the first one is, like we've seen, time and time again, change System.Data.Entity to Microsoft.EntityFrameWorkCore.
And then we are also going to want to add in the Initializers. So, that's SpyStoreDAL.EF.Initializers. We didn't need those in the old version because we did this Database.Initialize call, which ran the DB Initializer, the class that we deleted because it's not supported in the EF core.
So, the change we have to make here, we delete that line, 22. We then change that repo assignment to new ShoppingCartRepo, but remember we have to pass in the ProductRepo and that takes a context, we're passing in the context and here we're pass in true because we do want the Dispose to execute. And then, we call Initializer.
InitializeData passing in our stored context. One check that we want to do in the Dispose is just do a NoCall less, that way in case we are already disposed then it doesn't try and re-dispose it. If you're not familiar with X unit, there aren't any setup or tear down attributes. When a test class implements IDisposable, the constructor runs before every test and the Dispose runs after every test.
So, before every test, we're creating a new context, creating a new repo and putting the data back into the database, because remember InitializeData clears and then seeds the data. This ensures that every test the exact same data, so it can be a predictable test run. And, one other change we have to do. We scroll down to this method here, where we're creating a new ShoppingCartRepo and we just have to update the constructor to take in a new ProductRepo database and we should said, actually this is a good example; we don't need to say true here because we're within a using statement and it will automatically get disposed.
Now, let's run the test and make sure our code works. These test bubbles over here are re-sharper, I'm not going to use those because you can do it from within Visual Studio by going Test, Windows, Test Explorer and all of your tests will show up here in the Test Window Explorer. If your tests don't show up for you, make sure you do a clean and rebuild of your solution.
What I mean by that is right click on the Solution, Clean Solution, Rebuild Solution. Sometimes they get a little out of sync. So, we're going to click on Run All and we will watch while they execute and if they're all green, then we have a working Data Access Layer and we can move on to migrating the Web API 2.2 app to ASP.net core. We are all green and we are ready to go to the next module.
- Creating the .NET Core project
- Adding Entity Framework
- Migrating the data access layer (DAL)
- Configuring services and the HTTP pipeline
- Adding remaining services to the dependency injection container
- Migrating controllers and actions
- Testing the services