TestEnvironment - VianneyDoleans/Teia-Tests-Documentation GitHub Wiki

Test environment

To facilitate testing of Teia solution, a class was realized to be inherited by your test classes.
This class gives you the possibility to use a fake database, and so avoid to corrupt your own datadabse by mistake in order to test your teia solution.

DatabaseEnabledTestsBase

The class to used is named DatabaseEnabledTestsBase :

    [TestFixture]
    public class BlogServiceTests : DatabaseEnabledTestsBase
    {
    }

How it works ?

  • DatabaseEnabledTestsBase create a SqlCe database (SQL Server Compact)
  • This database is loaded inside your ram, and is cleared at the end of your test class execution
  • This database is empty, so you have to create the tables (see in method part) and have to be filled them in order to execute your tests

Methods

Here is the list of the available methods :

        [OneTimeSetUp]
        public void InitFixture();

        [OneTimeTearDown]
        public void TearDownFixture();

        [SetUp]
        public virtual void Init();
       
        [TearDown]
        public void Cleanup();
        
        public abstract void Register(ContainerBuilder builder);

        protected virtual IEnumerable<Type> DatabaseTypes
       
        protected void ClearSession();
  • InitFixture: have to be override to add initialization, this method is called only one time at the beginning, before the tests executions of the test class.
  • Init: have to be override to add initialization, this methid is called before each execution tests.
  • TearDownFixture: have to be override to add clean executions, this method is called a the end of test executions, so only one time
  • Cleanup : have to be override to add clean executions, this method is called after each test execution.
  • Register : Enables you to declare your injection dependencies (autofac). This method is called at the beginning. This method must be implemented.
  • DatabaseTypes: this virtual enables you to declare your record (tables of your database). It have to be override.
  • ClearSession : this method enables to clear your session at anytime, anywhere, you just have to call it.

What do you need to know ? (Tips)

Tree of records

  • In DatabaseTypes, you will have to declare the records you're using, but also the records of your dependency injections. If you forget to add them, the test class will throw an error.

String property too long

  • If you need an equivalent of nvarchar(max) inside your sqlCe database for tests, you have to add [StringLengthMax] attribute on your property declaration inside your record
    • example :
      • [StringLengthMax]
        public virtual string AccessTokenUrl { get; set; }
⚠️ **GitHub.com Fallback** ⚠️