Using unit tests and a test database
Asked Answered
T

2

5

How would I use NUnit and a test database to verify my code? I would in theory use mocks (moq) but my code is more in maintenance shape and fix it mode and I don't have the to setup all the mocks.

Do I just create a test project, then write tests that actually connect to my test database and execute the code as I wwould in the app? Then I check the code with asserts and make sure what I'm requesting is what I'm getting back correctly?

Timorous answered 7/12, 2012 at 22:37 Comment(1)
It sounds like you actually mean integration tests. Unit tests are isolated providing mock/fake implementations of dependencies to test a method they wouldn't have any database code in them.Vinaigrette
V
6

How would I use NUnit and a test database to verify my code? I would in theory use mocks (moq) but my code is more in maintenance shape and fix it mode and I don't have the to setup all the mocks.

Using mocks is only useful if you want to test the exact implementation behavior of a class. That means you are literally asserting that one class calls a specific method on another class. For example: I want to assert that Ninja.Attack() calls Sword.Unsheath().

Do I just create a test project, then write tests that actually connect to my test database and execute the code as I wwould in the app? Then I check the code with asserts and make sure what I'm requesting is what I'm getting back correctly?

This is just a plain old unit test. If there are no obstacles to achieving this, that's a good indicator that this is going to be your most effective method of testing. It's practical and highly effective.

There's one other testing tool you didn't mention, which is called a stub. I highly recommend you read this classic article for more info:

http://martinfowler.com/articles/mocksArentStubs.html

Velour answered 7/12, 2012 at 22:51 Comment(2)
Thanks, test db it is for me then. So just call the db with my code and test that its correct.Timorous
It might seem too good to be true, but it works. Especially considering the fact that your application has already been written.Velour
I
2

Since we are not talking about theoretical case, this is what I would do - From my understanding what you want to test is that whether your app is properly connecting to the DB and fetching the desired data or not.

  1. Create a test DB with the same schema
  2. Add some dummy data in that
  3. Open a connection to the DB from the code, request desired data
  4. Write assertions to test what you got from the DB against what you expected

Also, I don't think these tests should be called unit tests because they are not self contained and are dependent on other factors like whether your database is up and running or not. I would say they fall close to integration tests that will test if different components of your applications are working as expectation when used together.

(Dan's answer ^^ pretty much sums what I wanted to say)

Impress answered 7/12, 2012 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.