Xcode's unit testing framework should include a mocking library

Originator:ash
Number:rdar://34875844 Date Originated:8/10/2017
Status:Closed as dupe (5436582) Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:
 
When writing tests, I often have to create "mocks" of classes within my app. Mocking is a really common technique used in the Ruby and JavaScript communities but isn't used much in Swift or Objective-C. The most popular mocking library is open source: http://ocmock.org

So here's what I want from Xcode, out of the box: I want to be able to create mocks of a test subject's dependencies and use dependency injection to give them to the subject. Then I can run the code on the subject and assert that the mock received certain messages. It's cumbersome to describe a hypothetical syntax, so let me provide you with a few tests from a Ruby codebase I maintain.

it "handles swiftlint not being installed" do
  allow_any_instance_of(Swiftlint).to receive(:is_installed?).and_return(false)
  expect { @swiftlint.lint_files }.to raise_error("swiftlint is not installed")
end

it 'default config is nil, unspecified' do
  allow(@swiftlint.git).to receive(:added_files).and_return([])
  allow(@swiftlint.git).to receive(:modified_files).and_return([
    'spec/fixtures/SwiftFile.swift',
  ])

  expect_any_instance_of(Swiftlint).to receive(:lint)
    .with(hash_including(:config => nil), '')
    .once
    .and_return(@swiftlint_response)

  @swiftlint.lint_files
end

Equivalent tests written in vanilla XCTest are cumbersome, involve extensive subclassing and a tonne of boilerplate. The sad truth is: most developers don't write enough unit tests, and the lack of a decent, first-party mocking library would go a long way to improve this problem.

The static nature of Swift, and to some extent of Objective-C, make creating mocks a challenge. But I know you're up to it.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!