I have been writing a lot of unit tests recently, as we move the business logic from our reports into business objects that are then invoked by the reports. To ensure that the migrated business functionality works as expected, I've been writing unit tests to ensure that this is the case.
I came across one particular function recently that had my head scratching. The unit tests I wrote that invoked this particular function all worked yesterday, but then they all broke today. A quick scan through the code highlighted the obvious issue. The function returns the estimated mileage for today, given certain parameters. The function therefore determines what today is (the function is written in C# and so uses
Without modifying the code to the function, I was wondering if there was any way to unit test this particular function. The solution I went for in the end was to pass in a default value for today. If no value for today is passed in as a parameter (which is how the function is invoked by the reports) then today is assumed to be....well....today If a value is passed in for today (as is the case with my unit tests) then this value is assumed to be today.
The concern for me was that I had to amend my function to accommodate unit testing. Surely I shouldn't have to amend a function just so it can be unit tested.
Which got me wondering. Is there a way around this particular problem? Are there other scenarios where a change needs to be made to a function to accommodate unit testing?
I came across one particular function recently that had my head scratching. The unit tests I wrote that invoked this particular function all worked yesterday, but then they all broke today. A quick scan through the code highlighted the obvious issue. The function returns the estimated mileage for today, given certain parameters. The function therefore determines what today is (the function is written in C# and so uses
DateTime.Now
). So obviously the values returned from this function will return different results every day.Without modifying the code to the function, I was wondering if there was any way to unit test this particular function. The solution I went for in the end was to pass in a default value for today. If no value for today is passed in as a parameter (which is how the function is invoked by the reports) then today is assumed to be....well....today If a value is passed in for today (as is the case with my unit tests) then this value is assumed to be today.
The concern for me was that I had to amend my function to accommodate unit testing. Surely I shouldn't have to amend a function just so it can be unit tested.
Which got me wondering. Is there a way around this particular problem? Are there other scenarios where a change needs to be made to a function to accommodate unit testing?
"There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare
Home | LinkedIn | Google+ | Twitter
Home | LinkedIn | Google+ | Twitter