Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to organise or categorise unit testing DAO search methods
    text
    copied!<p>Firstly, forgive my use of the term "unit" when perhaps I mean integration test. However, in this case I am treating testing of a DAO method as a unit and am not attempting to mock the underlining database. </p> <p>I've been trying to test a particular DAO method that searches for particular entities - </p> <pre><code>public Factor GetMatchingFactor(int aircraftStoresConfigurationId, int stationId, DateTime timeStamp) { // code etc.... } </code></pre> <p>No, normally, I try to have several unit tests, maybe a few for each parameter to ensure each is treated appropriately. I'm happy with this normally when the method itself processes the parameter, and at worse calls a dependent that I can test using mocks / stubs. However, in this particular method, the result of the method is not a pure function or one parameter, but rather a function of the parameters AND the test data. </p> <p>Therefore, I struggle to define tests like</p> <pre><code>public void TestThatAircraftStoresConfigurationIdParameterIsApplied public void TestThatStationIdParameterIsApplied public void TestThatTimeStampParameterIsApplied </code></pre> <p>as their names are incorrectly, each does not just test one thing.</p> <p>This also means that I'm struggling to follow the rule of only one Assert per test. </p> <p>Therefore, I've tested this method using the following code with the same coverage, and possibly more meaningful. </p> <pre><code> [Test] public void TestReturnsCorrectResult() { Assert.That(_sut.GetMatchingFactor(10001, Station.Station9Id, new DateTime(2011, 11, 16, 10, 00, 00)).Id, Is.EqualTo(1), "Test 1"); Assert.That(_sut.GetMatchingFactor(10001, Station.Station9Id, new DateTime(2011, 11, 16, 11, 00, 00)).Id, Is.EqualTo(1), "Test 2"); Assert.That(_sut.GetMatchingFactor(10001, Station.Station9Id, new DateTime(2011, 11, 16, 19, 00, 00)).Id, Is.EqualTo(1), "Test 3"); Assert.That(_sut.GetMatchingFactor(10001, Station.Station9Id, new DateTime(2011, 11, 16, 19, 00, 01)).Id, Is.EqualTo(2), "Test 4"); Assert.That(_sut.GetMatchingFactor(10001, Station.Station9Id, new DateTime(2011, 11, 16, 19, 00, 02)).Id, Is.EqualTo(2), "Test 5"); Assert.That(_sut.GetMatchingFactor(10001, Station.Station9Id, new DateTime(2011, 11, 16, 14, 00, 00)).Id, Is.EqualTo(1), "Test 6"); Assert.That(_sut.GetMatchingFactor(10001, Station.Station10Id, new DateTime(2011, 11, 16, 14, 00, 00)).Id, Is.EqualTo(1), "Test 7"); Assert.That(_sut.GetMatchingFactor(10002, Station.Station11Id, new DateTime(2011, 11, 16, 14, 00, 00)).Id, Is.EqualTo(3), "Test 8"); Assert.That(_sut.GetMatchingFactor(10002, Station.Station12Id, new DateTime(2011, 11, 16, 14, 00, 00)).Id, Is.EqualTo(3), "Test 9"); } </code></pre> <p>I'm not comfortable with the multiple asserts, but it seems the only logical way to structure these tests. Can anyone suggest a better option?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload