2

I have been using method swizzling to swap implementations for unit testing. However, I am concerned that if the production code's method signatures change due to parameter changes, the unit tests will compile without error and testing run-time behavior could be unstable.

So, is there any compile-time or even run-time way to confirm that the signatures of two Objective-C methods are the same?

Jim
  • 21
  • 1
  • It's not a valid test unless the method signature in your test is the same as it is in production. – Robert Harvey Dec 11 '18 at 21:36
  • @RobertHarvey: are you saying that the unit test will always fail if it uses a swizzled method whose signature doesn't match production? – Jim Dec 11 '18 at 21:40
  • It won't run. How could it? – Robert Harvey Dec 11 '18 at 21:44
  • The unit test might crash...or maybe it won't. Such crashes have been problematic to track down in the past. So, the goal is to detect the method signature mismatch before swizzling. Anyone know a way? – Jim Dec 11 '18 at 21:47
  • OK, well clearly I'm not qualified to answer your question. I don't even know what "swizzling" means. Nor do I understand how a method signature could suddenly change in the way you described. – Robert Harvey Dec 11 '18 at 21:49

1 Answers1

1

As long as you have set up your unit tests such that the code is written as if you are calling an instance of the real class using its real interface, then a change in signature should be caught at compile time.

You could grub thru the runtime and get a hold of the method signatures and then compare the elements of said signatures for compatibility, but that won't catch all changes (for example, all parameters that accept objects are encoded as '@').

bbum
  • 162,346
  • 23
  • 271
  • 359