TheBasics - VianneyDoleans/Teia-Tests-Documentation GitHub Wiki
The basics
This section is regrouping the minimum of knowledge you need to know to start Teia project testing.
More details/explanations are given in the next sections.
Dependency injection
First, you have to know and understand Autofac.
Autofac is used for the depedency injections of Teia : constructors' arguments are injected instead of be declared on the method's call.
For testing the services, modules etc, you will have to create theses objects in a testing environment by using constructors and so redeclare the dependency injections.
- C# Dependency Injection with Autofac (video)
- This vidoo is really good because the narrator doesn't just give instructions, it explains how its works, why you have to do this, etc.
Note : Testing environment is a sandbox. At the beginning, there is nothing inside, you will have to declare and create what you need. So for each dependency needed for the object you're creating, you will have to declare the dependencies of this dependency (it gives a tree of dependencies).
Tests
Nunit
Nunit is an open-source testing framework used in C#/.NET environment.
Nunit was used by Orchard developers for orchard CMS developpment (started in 2011 and discontinued in 2019).
Current Teia solution is based on Orchard CMS, and so, used Nunit Framework.
There is many other frameworks for this purpose (MSTest, Xunit, ...).
- Although an important part of the current .NET community will prefer Xunit for the modern take in unit testing, Nunit keeps a solid and well proven status.
Documentation
To understand unit test operation, I recommend you to read theses articles :
- Specs and tests - vocabulary (french article)
- What is a unit test ? (french article)
- When and where to write unit tests? (french article)
- How does a unit test materialise? Concepts to reality (french article)
- NUnit Unit Testing Framework Syntax Sheet (article)
- Good article to have an idea of the Nunit possibilities and keep notes about the syntax
Mock
Okay so now you know how to make a unit test and how autofac works. The next step is to understand mock processes.
What is a mock ?
To understand the concept of Mock, I recommend you to read theses articles :
- "Mock or not mock ?" What is a mock and when to use it (concepts) (french article)
- Nice writing skills and give some good food for thought
- The concept of mocking
- Complete article to understand mocking concept
To complement the comprehension, these brief explanations resume the subject :
How to use Moq ?
In Teia solution, we are using Moq framework to realize your mocks.
- Mocking in C# Unit Tests - How To Test Data Access Code and More (video)
- Simple video to understand, but not everything have to be taken, because not exactly the same tech stack used in Teia (same narrator as a previous video)
- moq + Nunit examples
AutoFixture
Autofixture enables to automatically pre-populate your class, interfaces and data-types with test-data.
Autofixture in Teia's tests is used as a complement of Moq Framework (see previously) to realize our mocks.
What is AutoFixture ? How to use it ?
- Official project description (readme)
- Good presentation
- How you can use AutoFixture and AutoData to Drastically improve your Unit Tests (article)
- Nice explanations
- c# unit testing with AutoFixture AutoMoq in 4 min (vidéo)
- Demonstration code to see how to realize it
- What are the differences between MOQ and AutoFixture?