This is a simple example that shows the basics of mocking using the Moq framework. Feel free to fork the project and use it as a playground to develop your own ideas and tests using NHibernate and Moq!
The project reads the Books.json file and loads the records contained within into a small self-contained sqlite database. The models, mappings and the database accesses are built using NHibernate / FluentNHibernate. Feel free to check those parts as well as they could be useful for you in the future projects.
This project is a .Net Core 3.1 project so you can either import it into Visual Studio / Visual Studio Code and run it as you would normally, or you can use the terminal as well. You will of course need the .Net Core 3.1 runtime on the machine you are trying to run the project. (https://dotnet.microsoft.com/download/dotnet/3.1)
-
Navigate into the MoqDemo project folder and use the command
dotnet runto run the application. This will create the sqlite database, fill it with the data provided by the Books.json file and write the books and authors into the console output. -
Then, you can run the unit tests either from the root folder or from the MoqDemoTest folder using the
dotnet testcommand. This will run the unit tests and print the result into the console output.
If you want to read more about the possibilities of Moq and / or NHibernate, see the following links:
- Moq4 Quickstart guide: https://github.com/Moq/moq4/wiki/Quickstart
- NHibernate tutorial: https://nhibernate.info/doc/tutorials/first-nh-app/your-first-nhibernate-based-application.html
- NHibernate reference: https://nhibernate.info/doc/nhibernate-reference/index.html