Skip to content

Instantly share code, notes, and snippets.

@cojoj
Created July 2, 2015 20:22
Show Gist options
  • Save cojoj/de7e4457e5137de6e75f to your computer and use it in GitHub Desktop.
Save cojoj/de7e4457e5137de6e75f to your computer and use it in GitHub Desktop.
Guard coverage
// Got this method which makes call to API, blah, blah, blah...
self.sendRequestWithMethod(.GET, endpoint: .Repositories, params: nil, query: nil, body: nil) { (response, body, error) -> () in
guard error == nil else {
completion(repositories: nil, error: error)
return
}
guard let repositoriesBody = (body as? NSDictionary)?["results"] as? NSArray else {
completion(repositories: nil, error: Error.withInfo("Wrong body \(body)"))
return
}
let repos: [Repository] = XcodeServerArray(repositoriesBody)
completion(repositories: repos, error: nil)
}
// Given this test case
func testGetRepositories() {
let expectation = self.expectationWithDescription("Get Repositories")
let server = self.getRecordingXcodeServer("get_repositories")
server.getRepositories() { (repositories, error) in
XCTAssertNil(error, "Error should be nil")
XCTAssertNotNil(repositories, "Repositories shouldn't be nil")
if let repos = repositories {
XCTAssertEqual(repos.count, 2, "There should be two repositories available")
for (index, repo) in repos.enumerate() {
XCTAssertEqual(repo.name, "Test\(index + 1)")
}
XCTAssertEqual(repos[0].sshAccess, Repository.SSHAccessType.LoggedInReadWrite)
XCTAssertEqual(repos[1].writeAccessExternalIds, [ "ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050", "D024C308-CEBE-4E72-BE40-E1E4115F38F9" ])
}
expectation.fulfill()
}
self.waitForExpectationsWithTimeout(10.0, handler: nil)
}
@cojoj
Copy link
Author

cojoj commented Jul 2, 2015

If I run this test case I get code coverage like this:

Which is strange because setting breakpoint at let repos: [Repository] = XcodeServerArray(repositoriesBody) executes this breakpoint - so it's executed properly!


If I change guard style to if style coverage gets marked correctly:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment