new at unit testing and .NET Core (1.0.1), looking for some help.
Im looking to unit test a few classes that are using the .NET Core dependency injection pattern. Application runs fine, but when trying to run NUnit tests with the classes that require DI, error outputs:
No handler for message 'TestExecution.TestResult' when at state 'TestExecutionSentTestRunnerProcessStartInfo' An exception occurred while invoking executor 'executor://dotnetunittestexecutor/v1': Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. An existing connection was forcibly closed by the remote host
Haven't had a lot of luck with searching that error, or any documentation around using DI with NUnit. I have a feeling I'm not suppose to config like this. However, a few settings (connection strings etc) live in the appsettings.json file which get injected via Startup.cs. How would I access these in the NUnit classes?
My Startup.cs, gets the connection string and preps for DI
public void ConfigureServices(IServiceCollection services)
{
//other Configurations happen here
//get the connection string values from appsettings.json
services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
//Classes that get DI'd
services.AddSingleton<IMyDAL, MyDal>();
services.AddSingleton<MyBLL>();
}
MyDAL requires configurations to be injected, available via Startup.cs:
public class MyDAL : IMyDAL
{
private readonly string _dbConnectionString;
public MyDAL(IOptions<ConnectionStrings> connectionString)
{
_dbConnectionString = connectionString.Value.ConnectionString;
}
public bool DoDALStuff() { ... }
}
MyBLL class requires IMyDAL to be injected:
public class MyBLL
{
public readonly IMyDAL _myDAL;
public MyBLL(IMyDAL myDAL)
{
_myDAL = myDAL;
}
public bool DoBLLStuff() { ... }
}
My NUnit class requires the BLL injection to test:
[TestFixture]
public class BLLTest
{
public MyBLL _myBLL;
public BLLTest(MyBLL myBLL)
{
_myBLL = myBLL;
}
[Test]
public void TestTheBLL()
{
Assert.IsTrue(_myBLL.DoBLLStuff() == true)
}
}
This is how my regular program flow looks like, but I might be approaching it in the wrong way trying to implement this unit test in the same DI fashion. If I remove any DI/constructor code in the test class, the tests run fine, but I still need to test the methods that require DI inside the BLL. Anyone know how I can get the DI'd objects into my test class to use? Am I using an incorrect approach?