How to write an integration test in NUnit?

We are two students who are writing our bachelor's degree, and we have developed a Windows application that should help the restaurant in various communication processes. In fact, he should be able to provide information about the order from the moment the guest was sent, sent to him.

We skipped testing during development, but decided to write unit tests now. However, we found out that the most suitable test that we can write to our system is integration tests, because all the methods in our classes are associated with SQL stored procedures through LINQ to SQL. We know about using stubs to fake database dependencies, but when our database is already implemented with all the functions, we believe that this will give us more benefit for testing several methods together as an integration test.

As you can see from the code below, we tried to follow the guidelines for the unit test, but is this the right way to write an integration test?

[Test] public void SendTotalOrder_SendAllItemsToProducer_OneSentOrder() { //Arrange Order order = new Order(); Guest guest = new Guest(1, order); Producer producer = new Producer("Thomas", "Guldborg", "Beverage producer"); DataGridView dataGridView = new DataGridView { BindingContext = new BindingContext() }; order.MenuItemId = 1; order.Quantity = 1; //Act guest.AddItem(); dataGridView.DataSource = guest.SendOrderOverview(); guest.SendOrder(dataGridView); dataGridView.DataSource = producer.OrderOverview(); var guestTableOrder = producer.OrderOverview() .Where(orders => orders.gtid == guest.GuestTableId) .Select(producerOrder => producerOrder.gtid) .Single(); //Assert Assert.That(guestTableOrder, Is.EqualTo(guest.GuestTableId)); } 
+8
c # sql database unit-testing integration-testing
source share
2 answers

Yes, generally speaking, here's how to write a unit test / integration test. You observe some important recommendations:

  • Distinctive steps "Arrangement-Arrangement"
  • The test name describes these steps (perhaps at the end there should be something like "ShouldSendOneOrder", "Must" is usually used to describe Assert).
  • One test request.

I assume that you also obey other recommendations:

  • Tests are independent: they do not change the constant state, so they do not affect other tests.
  • Verification of realistic use cases: do not arrange constellations that violate business logic, do not make impossible actions. Or: simulate a real application.

However, I also see things that raise eyebrows.

  • It is unclear which are acting . I think some of the โ€œactionsโ€ refer to the arrangement step.

  • A method of type producer.OrderOverview() makes me suspect that domain objects are interacting with databases. If so, it would violate the maintenance of ignorance. I think there should be a service that represents this method (but see below).

  • It is not clear why dataGridView.DataSource = producer.OrderOverview(); necessary for the test. If so, this only exacerbates the most serious problem:

  • The business logic and user interface are confusing !!

    • A method like guest.SendOrderOverview() and producer.OrderOverview() is smelly : why does a domain object know how to present its contents? This should be the responsibility of the presentation (MVP) or the controller (MVC) or the presentation model (MVVM).
    • A method like guest.SendOrder(dataGridView) is evil . It associates the domain layer with the user interface infrastructure. This fixed dependency is evil enough, but of course you also need values โ€‹โ€‹from the grid view inside this method. Thus, business logic needs in-depth knowledge of some user interface component. This violates the do not ask principle. guest.SendOrder should have simple parameters that tell it how to perform its task, and the domain should not have any reference to any user interface infrastructure.

You really have to turn to the last point. Do your task to run this test without any interaction with DGV.

+13
source share

If you continue to bind sql in the class, your test is not a big problem.

You can use this method when the program logic is very simple, but I suggest you study The Repository Pattern as the logic becomes more complex.

+1
source share

All Articles