Quick: Catch thrown errors in tests

Created on 9 Oct 2015  路  11Comments  路  Source: Quick/Quick

If you are writing a test in swift 2.0 and use throws the test should expect throwing functions and fail automatically if the function throws.

func aThrowingFunction() throws -> String {
  return "something"
}

class SomeTest: QuickSpec {
  override func spec() {
    describe("hanlding throwing functions") {
      it("should compile when having function that throws") {
        aThrowingFunction() // <- this should compile. test should fail if function throws
      }
    }
  }
}
enhancement help wanted

Most helpful comment

Is this something that's still of interest or needed?

All 11 comments

Might also be nice to add something like Mockitos expects. It could look something like:

it("should be able to expect errors", expects: .SomeError ) {
  aThrowingFunction() // <-- test should fail if the function does not throw.
}

I agree that Quick should fail implicitly if a throwing function fails.

I'm personally not a big fan of the Mockito-style of error catching. I've never used that library, but it sounds weird to me a mocking library would support that on some test runner infrastructure.

Yes it sounds strange that Mockito should catch errors. I looked it up and it is actually JUnit that catches and asserts errors.

Is this error related to the issue?

AgentTests.swift:40:17: Cannot invoke 'it' with an argument list of type '(String, () throws -> ())'

I have this which shows that error:

describe("rejectWrongUser") {
    it("rejects a user that is not present in the database") {
        do {
            let user = try ApiConsumer.login("maxpayne", password: "1234567890")
        } catch ApiError.WrongUsernameOrPassword(let message) {
            print (message)
        }
    }
}

you need to catch all possible errors so you probably need a default

do {
 // something that throws
} catch  SomeError.Something() {
// do something
} catch default {
// you need to do this as well
}

If the closure in it would rethrow and catch all other errors your code might work.

Thanks, adding default case helped! Though I suppose default itself is a reserved word.

Is this something that's still of interest or needed?

I would love this too

+1 馃憤

+1

Just throwing my hat in to say this would be really nice as well. @ikesyo any progress here or anything I can help with? Would be happy to try to contribute a bit

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndrewSB picture AndrewSB  路  3Comments

sunshinejr picture sunshinejr  路  4Comments

modocache picture modocache  路  7Comments

ivangodfather picture ivangodfather  路  7Comments

ivanruizscm picture ivanruizscm  路  7Comments