Skip to content

How implement testing support for different than NUnit, xUnit or MsTest Framework

NightAngell edited this page Feb 23, 2019 · 9 revisions

There is one simple condition. Framework must have possibility to execute code before and after each test. If it not support that features and you still want use my lib with your favourite testing framework don't worry, you still can use my lib (see this).

  1. Create new class library project for .netCore (2.1+), name your project using this: SignalR_UnitTestingSupport[ name of testing framework ].

  2. Add AspNetCore.SignalR.UnitTestingSupport.Common nuget to your project (and your testing framework nuget).

  3. Create Hubs folder inside main project directory.

  4. Create HubUnitTestsBase.cs file inside Hubs directory.

  5. Copy this and paste to that file. Don't change class name (cause docs). Remove unnecessary comments. Add required usings.

public abstract class HubUnitTestsBase : HubUnitTestsSupport
{
    //Attribute to be sure that, somebody don`t call it manually (some testing frameworks required public methods,
    //but framework and only that framework should call this)
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] 
    //Method which is executed before each test
    public void HubUnitTestSupportSetUp()
    {
        SetUp();
    }
}
  1. Create HubUnitTestsBaseT.cs file inside Hubs directory.
  2. Copy this and paste to that file. Don't change class name (cause docs). Remove unnecessary comments. Add required usings.
public abstract class HubUnitTestsBase<TIHubResponses> : HubUnitTestsSupport<TIHubResponses>
   where TIHubResponses : class
{
    //Attribute to be sure that, somebody don`t call it manually (some testing frameworks required public methods,
    //but framework and only that framework should call this)
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] 
    //Method which is executed before each test
    public void BaseSetUp()
    {
        SetUp();
    }
}
  1. Create HubUnitTestsWithEF file inside Hubs directory.
  2. Copy this and paste to that file. Don't change class name (cause docs). Remove unnecessary comments. Add required usings.
public abstract class HubUnitTestsWithEF<TDbContext> : HubUnitTestsWithEFSupport<TDbContext>
     where TDbContext : DbContext
{
   //Attribute to be sure that, somebody don`t call it manually (some testing frameworks required public methods,
   //but framework and only that framework should call this)
   [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] 
   //Method which is executed before each test
   public void EfSetUp()
   {
      SetUp();
   }

   //Attribute to be sure that, somebody don`t call it manually (some testing frameworks required public methods,
   //but framework and only that framework should call this)
   [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] 
   //Method which is executed after each test
   public void TearDownEFContexts()
   {
       TearDown();
   }
}
  1. Create HubUnitTestsWithEF` file inside Hubs directory.
  2. Copy this and paste to that file. Don't change class name (cause docs). Remove unnecessary comments. Add required usings.
public abstract class HubUnitTestsWithEF<TIHubResponses, TDbContext> 
        : HubUnitTestsWithEFSupport<TIHubResponses, TDbContext>
        where TIHubResponses: class
        where TDbContext : DbContext
{
   //Attribute to be sure that, somebody don`t call it manually (some testing frameworks required public methods,
   //but framework and only that framework should call this)
   [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] 
   //Method which is executed before each test
   public void EfSetUp()
   {
      SetUp();
   }

   //Attribute to be sure that, somebody don`t call it manually (some testing frameworks required public methods,
   //but framework and only that framework should call this)
   [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] 
   //Method which is executed after each test
   public void TearDownEFContexts()
   {
       TearDown();
   }
}

Support for IHubContext is also implementend now, cause it`s fully implemented in: AspNetCore.SignalR.UnitTestingSupport.Common

Clone this wiki locally