Hi everyone. This is my first post to this community, so I hope I’ve found the right place to ask for help with my toy project.

I have been playing around with making an API using C# and I’m really having trouble figuring out how people test these kinds of things. My application is very simple right now. Basically I have a couple of tables that I want to act like single entities. To make an analogy, if my application is for books, my database has a table for books and a table for authors. When you call `myurl/api/books` you will get back something that looks like `[{book:{title:…, otherstuff: … }, author: {firstname:…, lastname:…}, {…}]`. Essentially a composite representation of the entry for the books table and the authors table. I am using Dapper to do all my database interactions and a simple WebAPIController for my controller with basic crud operations.
My question is how do I test something like this? I’m having a really hard time figuring out how to test that my Models classes accurately behave like the tables they represent in the db, testing that my repository objects (or data access objects) actually use a model object to query a table, and then to query a controller and get back the data I expect. I am not very experience with mocking and testing and this project is supposed to serve as a bit of an exploration into better testing, but I can’t figure out how to test something that has these kinds of layers. I feel like I somehow need to simulate a database for dapper to interact with. Can someone point me to an example of how this is done? Or a simple example of how you test the different layers of a web api? Thanks in advance!

I would put all your database queries in a repository using the repository pattern. I wouldn’t worry about testing your repository. That would be more of an integration test not a unit test.
Your repository should implementing an interface. Your controller or service layer would use dependency injection. Then you can mock your repository interfaces using something like fakeiteasy. I’m in mobile so I apologise for the short explanation.
This is a lot of things that I don’t know. Would you mind pointing me towards what the repository pattern looks like in this case? I don’t have any actual database queries. I am just using dapper.contrib for simple crud operations if that makes a different.
I have read about interfaces in programming, but I don’t really know what one looks like in this case, or what using dependency injection there looks like. I really think some sort of example for what the proper setup for a web api would be really helpful.
C# devs
null reference exceptions

source