jonreid / OCMockito

Mockito for Objective-C: creation, verification and stubbing of mock objects
MIT License
991 stars 118 forks source link

Added support for stubbing singletons #128

Closed igorsales closed 8 years ago

igorsales commented 8 years ago

A patch for the rest of us, who know it's easier to live with the reality that singletons are here to stay. While adding the ability to set those objects as properties, sometimes, it just makes sense to plainly use them. I hope this patch is good enough to get the ball rolling.

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-1.8%) to 96.627% when pulling 5e185b19b287c58c65c4d40c888a766fb35ce685 on igorsales:master into d93701ae36d35f0b77587a2f82cf03199cd753ba on jonreid:master.

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-1.2%) to 97.226% when pulling a786daf1f1022c334839edc992e778cf7a55cc96 on igorsales:master into d93701ae36d35f0b77587a2f82cf03199cd753ba on jonreid:master.

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-0.1%) to 98.285% when pulling 9278b325d8cc59d65cfee60036ee194cb28b9e0d on igorsales:master into d93701ae36d35f0b77587a2f82cf03199cd753ba on jonreid:master.

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-0.1%) to 98.285% when pulling 377fb80356cef86748f89fd575c1d82fc1a99e91 on igorsales:master into d93701ae36d35f0b77587a2f82cf03199cd753ba on jonreid:master.

coveralls commented 8 years ago

Coverage Status

Coverage decreased (-0.07%) to 98.352% when pulling 3379193dd6228846e5eb0fba99133fbfe33efff6 on igorsales:master into d93701ae36d35f0b77587a2f82cf03199cd753ba on jonreid:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.0009%) to 98.418% when pulling 3da7d4a714f7f514f9c2b81c122f8ddebfb8f5ba on igorsales:master into d93701ae36d35f0b77587a2f82cf03199cd753ba on jonreid:master.

coveralls commented 8 years ago

Coverage Status

Coverage increased (+0.0009%) to 98.418% when pulling 3887d1e4610dac8a608db1f140adedd6669a16e0 on igorsales:master into d93701ae36d35f0b77587a2f82cf03199cd753ba on jonreid:master.

jonreid commented 8 years ago

Trying to understand the code. Could you explain this unusual direct reference to the ivar in MKTClassObjectMock.m?

    //if (swizzle.mockedClass == self.mockedClass) {
    // At time of dealloc, it's possible the weak ref to swizzle.mock is nil,
    // so we also check directly on the struct member
    if (swizzle.mock == self) || swizzle->_mock == self) {

Or maybe I should ask, how did you discover the need for this?

igorsales commented 8 years ago

When I was developing it, the body of the if wasn’t being called, so I investigated why, and the problem is that sometimes (under a dealloc call) when that block is called, the ivar accessor (swizzle.mock) returns nil, but the ivar is still there.

Strange yeah, I didn’t expect it to work that way. I think it might have to do with compiler optimizations. When running in debugging, I think it would almost always go into the block, while during Unit Testing and production it wouldn’t.

I hope this helps, —i

On Jun 27, 2016, at 1:00 AM, Jon Reid notifications@github.com wrote:

Trying to understand the code. Could you explain this unusual direct reference to the ivar in MKTClassObjectMock.m?

//if (swizzle.mockedClass == self.mockedClass) {
// At time of dealloc, it's possible the weak ref to swizzle.mock is nil,
// so we also check directly on the struct member
if (swizzle.mock == self) || swizzle->_mock == self) {

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jonreid/OCMockito/pull/128#issuecomment-228654444, or mute the thread https://github.com/notifications/unsubscribe/AACXZQ5F6NEeVd8Gg1N0DHk0HpS1Gsdqks5qP1kCgaJpZM4IdJ51.

--i

e-mail: self@igorsales.ca twitter: @igorsales blog: https://blog.igorsales.ca web: https://igorsales.ca

jonreid commented 8 years ago

Thanks again for "getting the ball rolling"! The significant changes I made were:

I'll start preparing a release.

igorsales commented 8 years ago

Cool, thanks so much for accepting it!

Cheers, —i

On Sep 4, 2016, at 8:22 PM, Jon Reid notifications@github.com wrote:

Thanks again for "getting the ball rolling"! The significant changes I made were:

Did "extract class" to pull out MKTSingletonSwizzler Added error reporting if you provide stubSingleton() with incorrect arguments I'll start preparing a release.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jonreid/OCMockito/pull/128#issuecomment-244637143, or mute the thread https://github.com/notifications/unsubscribe-auth/AACXZUuu6God-qKlYaaiFPpMpLSl3m1Qks5qm2C2gaJpZM4IdJ51.

--i

e-mail: self@igorsales.ca twitter: @igorsales blog: https://blog.igorsales.ca web: https://igorsales.ca