Skip to content

Unit and integration testing

Jon P Smith edited this page Aug 26, 2020 · 2 revisions

This covers unit testing your business logic code outside GenericBizRunner and integration testing on Web APIs that use GenericBizRunner.

Unit testing your business logic

You can, and should test your business logic without using GenericBizRunner. You just create an instance of your business logic and go from there. Have a look at this unit test as an example.

NOTE: remember that GenericBizRunner calls SaveChanges, so if you want to check the database update then you need to call SaveChanges in the unit test.

Integration testing

In one of my client projects they used GenericBizRunner with Web APIs and they wanted to integration test those Web APIs. This uses the Non-DI approach to configuring GenericBizRunner. Here is an example xUnit integration test taken from EfCore.GeneriServices.AspNetCore support library.

public void TestCreateTodoOk()
{
    //SETUP
    var options = SqliteInMemory.CreateOptions<ExampleDbContext>();
    using (var context = new ExampleDbContext(options))
    {
        context.Database.EnsureCreated();
        context.SeedDatabase();
        var controller = new ToDoController();

        var noCachingConfig = new GenericBizRunnerConfig { TurnOffCaching = true };
        var utData = new NonDiBizSetup(noCachingConfig); //Use this if the method doesn't use DTOs
        var bizInstance = new CreateTodoBizLogic(context);
        var service = new ActionService<ICreateTodoBizLogic>(context, bizInstance, utData.WrappedConfig);

        //ATTEMPT
        var dto = new CreateTodoDto()
        {
            Name = "Test",
            Difficulty = 3,
        };
        var response = controller.Post(dto, service);

        //VERIFY
        response.GetStatusCode().ShouldEqual(201);
        var rStatus = response.CheckCreateResponse("GetSingleTodo", new {id = 7}, context.TodoItems.Find(7));
        rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors());
        rStatus.Message.ShouldEqual("Success");
    }
}

See the NonDiSetup class for the list of methods you can use.

Clone this wiki locally