# Test a method that Swizzles in Swift [closed]

How can I test a class that Swizzles? In particular, I want to test the following:

let bundleName = "Bundle.bundle"

class Localizer: NSObject {
class func swizzleMainBundle() {
MethodSwizzleGivenClassName(cls: Bundle.self, originalSelector: #selector(Bundle.localizedString(forKey:value:table:)), overrideSelector: #selector(Bundle.specialLocalizedStringForKey(_:value:table:)))
}
}

extension Bundle {
@objc func specialLocalizedStringForKey(_ key: String, value: String?, table tableName: String?) -> String {
if self == Bundle.main {
return (bundle.specialLocalizedStringForKey(key, value: value, table: tableName))
}
return (self.specialLocalizedStringForKey(key, value: value, table: tableName))
} else {
return (self.specialLocalizedStringForKey(key, value: value, table: tableName))
}
}
}

func MethodSwizzleGivenClassName(cls: AnyClass, originalSelector: Selector, overrideSelector: Selector) {
if let origMethod: Method = class_getInstanceMethod(cls, originalSelector), let overrideMethod: Method = class_getInstanceMethod(cls, overrideSelector) {
if (class_addMethod(cls, originalSelector, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
class_replaceMethod(cls, overrideSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, overrideMethod);
}
}
}


Which changes the localisation bundle for a local bundle that I have created within the user directory (when used correctly).

Do I need to rewrite this class to make it testable? I really have no real idea how to make it easily testable.

• We can review code that you have already written. However, if you're asking about how to write a test, then that's asking for code to be written (or advice about how to write such code), which is off-topic for Code Review. – 200_success Oct 7 '19 at 23:43
• Can you make this question clearer? are you asking if the code is written well enough to be easily testable? if you are asking if the code is written well and within standards that sounds like a code review, but if you are asking us to write test cases for this code, that is as @200_success has noted, off-topic for Code Review. – Malachi Oct 8 '19 at 0:06
• I can't test this, I believe it to be untestable. Therefore the code review would be to write testable code for the functionality above. – stevenpcurtis Oct 8 '19 at 1:03