Need to write unit testing for the following code, I want to do mock for class method canMakePayments, return yes or no, so far no good method found dues to canMakePayments is a class method (+), seems all OCMock methods are all used for instance method (-).
You guys any suggestion or discussion will be appreciated. Thanks.
// SKPaymentQueue.h
// StoreKit
if ([SKPaymentQueue canMakePayments]){
....
}
else{
...
}
Since you can't intercept the method by providing a different instance, what you can do for a class method is provide a different class. Something like this:
The point of call then becomes:
This introduces a "testing seam," or a point of control, allowing us to specify a class other than
SKPaymentQueue
. Now let's make a replacement:Strictly speaking, this isn't a "mock object" -- it's a "fake object." The difference is that a mock object verifies how it's called. A fake object just provides stubbed results.
Now let's create a testing subclass of the original class we want to test.
So you see, this replaces
SKPaymentQueue
withFakePaymentQueue
. Your tests can now run againstTestingSubclass
.One approach is to wrap the class method in your own instance method:
Then you mock that method: