AyBayBay
AyBayBay

Reputation: 1724

OCMVerify/Expect in Swift?

I would like to Verify that an expected method is called with the correct parameters in Swift in unit-testing. This was very easily done in Objective-C using the OCMock framework. You could do a combination of partialMocking and running OCMExpect/Verify to assert code paths with the right parameters were being called. Any idea how to do something like this in Swift?

Upvotes: 1

Views: 646

Answers (1)

Jon Reid
Jon Reid

Reputation: 20980

Swift doesn't support reflection, so traditional mocking libraries aren't feasible. Instead, you need to create your own mock. There are at least two approaches to do this.

  1. Create a testing subclass of the class under test. This is partial mocking. Avoid this if possible.
  2. Use an interface instead of a class. Create a testing implementation of the interface.

Hand-crafted mocks aren't hard. You want to

  • Count the number of calls to a method
  • Capture its arguments
  • Simulate its return value

It is a lot of boilerplate. There are libraries out there that can auto-generate this code.

Upvotes: 1

Related Questions