Everybody can appear with deps slopes dot.com and in this video we're going to be talking very quickly
about the apps state when you're testing.
Now of course if you have an app that deals with something like core data or user defaults persisting
data to an app of course it persists.
So it stays there even if you quit the app and come back in.
So what we're going to be doing is we're going to be looking at how to actually clear out that persistent
data when testing so that we can have a fresh app state every time.
So pull open your project here this is where we left off last time and I'm actually going to go ahead
and get rid of this test with the alert because we don't actually need that because you know we're not
going to actually have that alert.
And with that in mind we can also get rid of this code so that it just sets user defaults and then dismisses.
Now basically what I have done I already sort of explain this but I've basically set a parameter in
user defaults that is based off of this extension here.
And it's a parameter called onboarding comp..
And basically it's a value that gets the property here from wherever it's called and it's then set in
user defaults.
Using this nice little extension whenever we call it.
Pretty cool.
So we're going to set it to true when the onboarding is done and it's set false by default.
So when we load up our VC here it's going to basically say Hey check it out.
If user defaults onboarding complete is not true will present the onboarding VC.
Otherwise don't worry about it.
So what we're going to do is we're actually going to make sure that every time our app launches we have
a fresh state and to do that we're going to go into the app delegate and we're going to be putting a
little function right here up at the top called reset state.
Now of course that function is not yet written.
But what we're going to do is we're going to actually utilize something called Konsole arguments.
OK.
Now in our tests what we're going to need to do is we're going to actually need to set it up so that
when ever our tests run we can basically append a little message to the end of our test to kind of set
it apart and and give it an identifier so that we can identify that specifically in our app delegate.
Let me show you what I mean and we're basically going to say in the setup function every single time
app dot and there is a property here called Lunch arguments.
OK.
Now these are the arguments that will be passed to the application at launch.
So go ahead and call apt at launch arguments and we're going to append a string to the end of every
argument.
So type dot append and we can add a string.
So what we're going to do is we're going to append this dash dash UI testing.
And that way every time a particular launch or every time our app launches we end up with this appended
to the launch arguments.
And what that is going to allow us to do is in our app delegate every time the app finishes launching
we're going to go ahead and check if command line k that's a class we can access command line dot arguments.
Ok this is where they print out Dom contains and check it out we can type in dot dash dash UI testing.
If the command line arguments ever contain UI testing we know that we need to reset the state and the
cool thing is that every time our tests run this we'll get called once so we'll say hey the app launched
UI testing reset the state.
So every time it launches it'll have a fresh state.
Now let's actually write this function because right now it does nothing.
So down at the bottom of your app delegate here type phunk reset state and let's give some more space
here at the bottom.
Let's make sure that we can actually see everything.
And first we're going to create a property that basically holds our user defaults and then we're going
to go ahead and remove the.
So basically we're going to create a property that has the name for our user defaults the bundle identifier.
And we're going to go into our user default and actually remove everything that we've saved for that
particular bundle ID so type guard let bundle.
Identifier equals bundle dot main dot bundle ID or bundle identifier else.
If that doesn't work then we're going to have to call fatal error and which will basically stop our
application if we can't do that then we shouldn't be continuing.
Next we're going to go ahead and type user defaults dot standard.
That just gets the user defaults.
And we're going to go ahead and type remove persistent domain for a particular name which in this case
is our bundle ID.
So go ahead and type bundles ID and that will basically reset the states the reset the state of our
application by removing all user defaults for this particular apps bundle ID so we can now call reset
state whenever our app launches.
And if I go back into my tests this test should now pass because when this test runs it goes all the
way through.
It checks to make sure that this exists then it's going to go ahead and go through the next test and
it will make sure that it gets a fresh state and that it won't crash.
So to just verify that this actually works I'm going to put a new temporary test in here.
I'll just say test temp and what it's going to do is it's basically going to swipe left and then tap
done which should dismiss the screen there.
We're not actually testing anything here because we're not asserting.
But what it's going to do is it's going to basically go through the process of tapping done setting
user default.
And then when this test runs you'll see what I mean that we get a fresh state every time.
So first call apt at launch I guess we need to actually launch the application in order for it to do
anything.
Then go ahead and click the big diamond up at the very top of the class declaration and we'll run all
of our tests and let's see how we do.
Here we go.
So we have our simulator and here we go the first test is going to run the app will launch we're going
to check is this text here.
Swipe check.
Well I guess it ran the bottom one first so that passed that's good.
Now when we open it we should have a fresh state and look at that it's fresh We have our onboarding
you pop up.
That's awesome.
So now our third test is going to run swipe swipe and dismiss.
And our third test passes that is so cool.
So now every time we are resetting the apps state this is a really really really really good way to
make sure that you have a fresh brand new instance of your application every time.
Of course this is just for user defaults.
If you're using something like core data you would want to clear out all entities of your core data
every single time so that the app launches with a fresh set of data.
Very very cool stuff.
So we have now learned how to actually manage the apps state and reset it so we get a fresh one every
single time.
Very very cool stuff.
Let's go ahead and head over to the next video and keep learning about UI testing.
I'll see you there.