Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring parameters and unit tests

I have this method:

public bool CanExecute()

And after 70 commits, I added an extra parameter

public bool CanExecute(IStation target)

Now the problem is I have 7 unit tests covering this CanExecute methods testing various nulls/property combinations.

Adding this simple parameter required a fix of those 7 unit tests. The fix is simple but...

Is there a best practice and/or pattern to avoid this kind of hand-refactor required to update unit tests?

Suppose I know an extra parameter might be added in the near future, how'd I code the unit test to account for that? Is it just overkill or is there an idiom/pattern/something to follow?

EDIT: I could not simply add an overload because the IStation dependency is not optional. I was fixing a bug where an IStation instance was expected but none was available so it must be supplied via CanExecute... you see.

Refactoring tools seem to be the way to go. Thanks!

like image 812
chakrit Avatar asked Feb 06 '26 09:02

chakrit


1 Answers

Could you not keep both methods in the code? Unless it is mandatory for the IStation parameter to be non null then you could get away with it without changing any existing code.

Alternatively if the parameter has a sensible default (again, like null!), resharper can take care of changes like this very easily. To add a new parameter, right click the function name and select Change signature... From here you can add new parameters with sensible defaults. RS will update all the calls so you don't have to !

like image 154
Jennifer Avatar answered Feb 07 '26 23:02

Jennifer