2

I want to use mock framework for swift language in iOS. I have used OCMock framework for Objective C, But same was not working in swift. So, I need proper frameworks for handling mock object in swift.

2 Answers2

0

It's common to have to implement 'mocking' through fakes or test doubles in Swift. Either through implementing protocols or subclassing in your tests.

This kind of mocking leads towards design patterns like dependency injection where you can test portions in isolation.

Things like OCMock or Kiwi relied on objective-c's runtime. Swift has yet to implement reflection as it is still working out its ABI stability. Until that comes, manually implementing mocks is the way to go.

0

We can always do:

  • Hand rolled Protocol based mocks

The Mocking framework options as of 2023, in no particular order:

The key benefit of the frameworks is that they'll auto generate mocks, the key drawback is they auto generate mocks (which is fine until you need to figure out why X goes wrong.)

For most cases, protocol based hand rolled mocks + constructor dependency injection, is probably fine for most unit testing, assuming you're isolating things and doing the SOLID work.

The frameworks might help when you need a team to follow a specific practice. Each will introduce their own cognitive overhead and wrinkles.

The frameworks are (allegedly) also going to make light work of mocking Apple objects, UIView, NSView etc. (certainly less brittle and less overhead than a hand rolled mock. Providing the framework doesn't introduce a testing-heisenbug)

There's a lot of trade-offs, all options should be carefully considered and taken for a test drive. No one-size fits all solution that I'm aware of, yet.

ocodo
  • 29,401
  • 18
  • 105
  • 117