How to write unit test in iOS for expected assertionFailure?

UPDATE:

It seems that this functionality should be supported in future versions of Swift 2.0 / XCode 7, which are likely to include try / catch support, so this question is likely to be controversial. I will try to update this post accordingly when they are not in beta.

ORIGINAL QUESTION:

In Swift, although I assume that the question / answer will be applicable to Objective-C, I want to write the foo function in the format:

 public class SomeClass{ public func foo(someString:String){ //validate someString assert(!someString.isEmpty, "The someString parameter cannot be empty.") } } 

I use the assert call because I believe that this is what Apple recommends, rather than throwing exceptions, as is customary in other languages.

However, in my unit test, I want to make sure that the function does not really work when the someString parameter is an empty string:

 class SomeClass_Tests:XCTestCase{ func test_foo_someStringParamaterIsEmpty_error(){ //ACTION let someClassInstance = SomeClass() someClassInstance.foo("") //VALIDATE //**What goes here? } } 

I cannot find documentation or messages about this situation, although I believe that this is a very important unit test to ensure proper behavior and use of classes and libraries.

In other languages, which include exception / exception handling, assert will be replaced with something like throw SomeError() , and then in unit test you can simply transfer the action to a try / catch and claim that the exception is really set like this:

 class SomeClass_Tests:XCTestCase{ func test_foo_someStringParamaterIsEmpty_error(){ //ACTION let someClassInstance = SomeClass() var expectedException:SomeException? = nil try{ someClassInstance.foo("") }catch(someException:SomeException){ expectedException = someException } //VALIDATE XCTAssertIsNotNil(expectedException) } } 

But there are no such designs or equivalent jobs in Swift that I saw in the documentation. Are there any known solutions or workarounds for performing such tests?

+7
ios unit-testing assertions swift xctest
source share
4 answers

However, in my unit test, I want to be able to ensure that the function really fails when the someString parameter is an empty string

I understand what you are trying to do, because I did it myself in Ruby programs. But Swift (as I often observed) is not Ruby! The problem here is that you can guarantee that the function will not work if the someString parameter is an empty string in your real application. This is because claims do not work in release builds.

As a result, you can use assert as a form of debugging during development, but if such a case can happen in real life, you should handle it in good condition, not crash.

And thus, testing whether the statement is β€œhappened” is really not a valid unit testing method, so you have trouble using it that way.

0
source share

You can check out the XCTAssertThrows and XCTAssertThrowsSpecific macros available as part of the XCTest.

You may need to replace assert with [NSException raise] to make it work.

0
source share

why try to catch a mistake and check what happened, instead of checking this statement?

 XCTAssertThrows() 

and how to see the values

0
source share

Matt Gallagher The CwlPreconditionTesting project in github adds a catchBadInstruction function that gives you the ability to test assertion / precondition errors in unit test code.

The CwlCatchBadInstructionTests file shows a simple illustration of its use. (Note that it only works in the simulator for iOS.)

0
source share

All Articles