TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
Effective .NET Core Unit Testing with SQLite and Dapper
1. EFFECTIVE .NET CORE UNIT TESTING
WITH SQLITE AND DAPPER
Mike Melusky – Philly.NET
November 17, 2018
2. ABOUT SPEAKER
• Michael Melusky
• Software Developer for Audacious Inquiry in Baltimore, MD
• Adjunct Instructor at Penn State University
3. TOPICS
• Motivation for this talk
• Apply testing to a “real world” . Netcore C# example
• Closing thoughts
4. CODE FOR TODAY
• I have a starter project available on Github:
• https://github.com/mrjavascript/phillynet-codecamp-2018-2
• All code for today will be pushed to this repository
• Slides on SlideShare (mrjavascript)
6. STACK OVERFLOW DEVELOPER SURVEY
• Each year Stack Overflow polls developers, asking questions to gauge
popular languages and frameworks and technologies
• Jetbrains (the company behind IntelliJ and Resharper) started publishing
their own survey as well
• There was an alarming survey response to a question regarding unit
testing in the Jetbrains survey
7.
8. WHAT IS UNIT TESTING?
• Testing individual units of code
• Numerous frameworks are available for this:
• Java:
• jUnit / Mockito
• .NET:
• xUnit, nUnit, mstest, Moq
• Javascript:
• Jest, Cucumber, Mocha, Enzyme, Protractor … (many, many, many, many
more)
9. JAVA MAKES
IT EASY!
• Spring Framework
recommends having two
separate source trees, one
for “main” and one for
“test”
• No excuse to not do unit
testing
10. WHAT ABOUT .NET?
• About me, I’m a “Java Developer” by trade
• Code is written in Java 8 with Spring Boot 2
• Production Database is PostGreSQL 10
• Uses jUnit and Mockito for testing frameworks
11. BUILDING A RESTFUL API
• For building a RESTful API or backend application, generally the
application is layered into the following components:
• Controller – handles HTTP request and responses from the clients
• Service – applies transactional business logic and validation
• Repository – data access layer (SQL or ORM like Hibernate or Entity
Framework)
12. JAVA UNIT TESTS
• As mentioned, the live data exists in a production PostGreSQL database.
• For the unit tests, the same code is ran against a different in memory
database (in this case H2)
• I recently had to build a .NET Core 2 application which connects to
Microsoft SQL Server
• I wanted to apply the same testing strategies used with .NET (data
access layer code queries SQL Server in production, but uses a in
memory database for tests)
14. XYZ WIDGET CORPORATION
• Assume we recently were hired to work for XYZ Widget Corporation
• Our first task as an employee is to find a way to integrate unit testing
into a new .NET Core application another developer wrote
• ** DEMO **
15. .NET UNIT TESTING FRAMEWORKS
• The following options are available for writing unit tests with .NET Core
• Nunit
• MsBuild
• Xunit
• I’m using Xunit for these examples (personal preference)
17. LETS INTEGRATE XUNIT INTO CODE
• Before we do this, we need an in-memory database to run the unit tests
against
• For these examples, let’s use SQLite
• ** DEMO **
19. TEST DRIVEN
DEVELOPMENT
• A paradigm where the unit
tests are written before the
application code
• .NET Core makes this easy!
• This might be a utopia for
some applications however…
20. BEST PRACTICES
• Run code coverage reports!
• Make unit tests quick so developers get into the habit of running tests
before pushing code
• If you receive a JIRA ticket for a bug fix, write unit tests to verify your
changes!
• ** DEMO CODE COVERAGE **