In this lesson we'll cover how to work with tests and how to distinguish between warnings and errors in Xcode.
- Learn how to work with Tests
- Learn the distinction between warning and errors and how to navigate to them with the issue navigator.
NOTE - The test file might look different from the screenshots below (for now). The reason for that is that we updated our curriculum to work with Swift 3.0 and Xcode 8 which is the latest version as of 9/13/16. When the Quick & Nimble framework have been updated to work with Swift 3.0 (which should be soon!), the screenshots should reflect with what is in the Xcode Project and this note will be removed.
Throughout this course, you will find that some labs (to be completed by you!) include tests. What are they and why are they useful? Tests are made to ensure that you are implementing the functions correctly.
Instructions might look like this:
- Create a function called
unicorn()
that takes in no arguments but returns aString
. TheString
that should be returned is a bunch of unicorns, to be exact.. it should be four Unicorn emoji's separated by a space. - Create a function called
loveAgain(_:)
that takes in aString
as an argument which represents an individuals name. This function will also return aString
. It should take in the individual's name and add a heart emoji to the end of it and return thatString
.
Your workflow for solving labs will require that you read through instructions just like the ones above and create those functions.
Instead of pressing (command + r) to run your app, you should press (command + u). This will instead run the tests.
If you implemented these functions correctly, this is what you should see.
Notice the green check mark to the left of line 13, it means we implemented both our functions correctly. Let's now jump to the ViewController.swift
file so you can see how we implemented these functions.
We know our test file (ViewControllerTests.swift
) is looking for us to implement these functions correctly. We know we run these tests by pressing (command + u), but what happens if we didn't implement these functions correctly. Lets instead of returning back a unicorn emoji, return back the string literal "I hate unicorns". So now our implementation looks like this:
func unicorn() -> String {
return "I hate unicorns"
}
Our Xcode project now looks like this:
If we were to now run the tests (by pressing command + u), this is what we would see:
It's letting us know that the tests haven't passed, indicated by that red x in the upper left there. But that doesn't help us much in figuring out how our function is not correct? How can we tell?
You can either click the file to the left of that mini red-x which would take you to the ViewControllerTests.swift
file or you can just get back to the ViewControllerTests.swift
file by clicking the folder icon in the project navigator and locating it.
If we get back to the ViewControllerTests.swift
file we should see this (which is very helpful):
-
Here, we can see that it's expecting the
unicorn()
function to return back aString
of four unicorn emoji's separated by a space, not theString
"I hate unicorns". -
It will be very helpful for you moving forward to reference the test files and see what it is they're expecting of you.
-
Included in this repository is the Xcode project if you would like to navigate through the files and see how it works.
When using Xcode, you're going to encounter warnings and errors. This is a normal part of the development process, so don't be alarmed when you see these symbols: they're there to help you!
Warnings and errors are generated by the pre-compiler (though often referred to as just "the compiler"), which is a tool built into Xcode that continuously scans through the code as it's edited in order to identify problems.
A warning is a minor issue that something unexpected might be caused by the code, but won't necessarily cause a crash at run time. Warnings will not impede the scheme being built.
An error is a major issue that will almost certainly cause a crash at run time, so the error impedes the scheme from being built.
Like all of the tools bundled together in Xcode, the pre-compiler is imperfect. It isn't able to identify every possible problem that might come about, and the messages that accompany these warnings and errors aren't always direct. This often leads Xcode to generate cryptic messages and to suggest incorrect fixes.
That being said, if your watchdog starts barking, investigate. In other words, don't simply ignore warnings and errors that Xcode presents to you. They're indicators that you may have done something undesirable, signs that your program might behave in a way that you don't want it to. So look into them. Keep your code clean from issues. (Eventually you'll begin working with other programmers in groups who shouldn't be burdened with having to sift through the warnings meant for you when working on their own features in the same project.)
Here's an example of the compiler disallowing us from changing the value of a constant. Because favoriteActor
is a constant, as it's declared with the let keyword. We shouldn't be allowed to change the value to "Brad". The compiler enforces this rule as you can see below. It is even giving us a suggestion. It's telling us to declare favoriteActor
using the var keyword which would make it a variable, thus allowing us to change its value to "Brad".
let favoriteActor = "Leo"
favoriteActor = "Brad"
View Xcode: Warnings and Errors on Learn.co and start learning to code for free.
View Testing and Xcode Warnings on Learn.co and start learning to code for free.