Hey everybody what's going on.
Caleb with Debb slopes just continuing the last video it started to get a little bit long.
And basically what we're doing is we're writing test for our data model we're basically going to check
to make sure the values are how they should be.
We're also going to basically test some of our functions and features.
And then in the following video we're actually going to continue testing functions and features.
I'm going to show you a few things.
You know things you might want to test you might want to think about.
And let's go ahead and pull up in a project so we left off testing the initialization and making sure
that when we give our instances values that they actually take them and their set.
We also set up a protocol stub to allow our struct to be equatable meaning we can directly correlate
between two instances of our custom class which is pretty or custom struct which is pretty cool.
So let's let's go ahead and write a test.
Now I want to write a test to basically check to see that these values come in as zero.
When I started so phunk test values.
That's what we're testing the values are initially 0.
So let's go ahead and let's say ex-city assert equal and we're going to go ahead and use our system
under test cases we have an empty instance we just created a blank instance.
And so I can type as the dot value a.
And it should be equal to zero.
Now we're going to assert the same thing ex-city assert equals Sutt value B.
And we're going to make sure that is also zero.
So if I build this the little test diamond will appear if I click on it the test will run and we'll
see how we did.
We're going to make sure that when we create a brand new instance that those test values come in as
zero.
And let's see the test is running shouldn't be taking this long.
Oh there we go.
Sorry I had to reopen the simulator.
Awesome.
So our test passes that means that these values do come in as zero.
Now you're probably wondering now do I need to test everything and.
Well as the app currently works it depends on these values being 0 from the very beginning.
And so if someone were to come in and change that these tests would then fail showing hey there's a
problem.
The way that our app works on a foundational level has been changed so it's really important to test
as much as you can but ultimately realize that it's not kind of a make or break thing if you don't test
every single single single thing.
It's probably going to be fine but it's always good to err on the side of testing more than less.
So go ahead and let's do another one here.
We're going to test if inserting a value actually changes the value of a.
So phunk test values inserting values changes value a K so we have our system under test which is our
calculation manager so Sutt.
And if I call the insert function what that's going to do is it's going to change value according to
my code.
So I'm going to pass in a value.
How about 10.
OK.
And we're going to go ahead and say ex-city assert equal sut value a should now be equal to 10.
And remember every single time we set it to nil and we create a brand new fresh instance of calculation
manager.
So let's go ahead and let's run this test with commend you.
It's going to run every single one of our tests which we probably don't need to do.
We just needed to run that one.
But anyway good to make sure my other tests still pass and look at that.
It passes when we insert a value.
The value is properly set.
Very cool.
So next you know we could check value b we know it's going to work the same way but we should check
to see if the operand is nil every time we create a new instance so phunk test operand wups is initially
no.
So let's just say ex-city assert nil
ex-city a certain nil there it is.
And we're going to say sut current operand build to show the diamond and then click on it to run just
that one singular test and hopefully it will pass because it's nil when we first create it.
Awesome.
And we have not set a value in our instance so that's perfect.
It is in fact nil as it should be it's not going to carry over from a previous calculation.
And now let's let's actually test setting the operand.
So we got to kind of think about the user process here and we're going to well let's write the test
first so phunk test operand K sets operand you know what let's do a better name.
How about when given operand sets operand.
OK.
And so what we're going to do is we're going to go through the process here.
We're going to say hey let's insert a value right because we type you know maybe 10 plus that the upper
end so let's do it like the user would they would type.
They wouldn't type this but what would happen is we would insert a value of maybe 20 and then we would
type sut dot and we would add the operand and we have a function for that set operand.
So we can set the opera and maybe I'm going to do 20.
Subtract something else.
And what I can do now is I can test ex-city not nil and I can verify that our operand is set a value
it's not nil anymore so I can type S U T.
Current operand build it.
And now we can run this test.
So let's make sure that our operand actually now has a value set.
It's testing everything and boom the test comes back a success.
Now if I were to ever basically set this to have a value like maybe I wanted to set it to be multiply
initially that would be a really bad thing to do.
But basically if I did that and tried to run all of my tests now watch what happens.
It's going to go ahead and launch my tests.
I changed my code and how it works and look at that.
One of our tests now fails it says Hey hey it's it's not that's not what it's supposed to do.
Man what's wrong with you.
So that's awesome.
But there is something that we could be doing it's not necessary but this message here is not very descriptive.
So what we can actually do at the end of any assertion at the very end we can type a comma and then
insert a string basically describing what is supposed to happen.
So we should say should or maybe have at this operand should be nil.
And now when I run the tests check out what happens it's going to run it's going to launch our tests
they will fail and then check it out.
When I look at the error message it's going to actually print out what should be happening.
Operand should be nil.
So if I get rid of this and then go back and run all my tests again that test will now pass pretty cool
how this works.
I'm sure you can see that this is really really really helpful stuff.
There we go past.
So let's write one last test here to make sure that when we actually perform a calculation that it actually
does what it's supposed to do.
So go ahead and type phunk test calculation for.
Well let's just say for values.
So we're going to go ahead and go through the whole process we're going to insert two values.
We're going to set the operand and then we're going to call our calculate function down here which returns
an integer value.
So type set.
Insert and let's do maybe 15 sut insert.
And what's the next value.
How about gee I don't know how about five.
OK.
And you know what we should we should call a set operand here and let's do subtract.
So we imagine we're on the calculator we type 15 subtract 5 and then we want to call calculate and we're
going to set that when we push the equals button later.
But what we can do is we can basically call sut dot calculate and that's going to return an integer
to us with whatever the calculation value is.
But of course we're not using that call.
So we should use X TC assert equal and we should basically pass in such to calculate loops like so.
And then we're going to want to make sure that it actually equals what we think it should.
So right now 15 minus 5 should be 10.
When we run this test it should work and you should check this with all the operands.
This is just one example of a test.
So I'm going to build it.
I'm going to click my little test icon here and let's see if our code is good if our test work.
That means that we're ready to move this into production.
Awesome.
Look at that it works very very cool.
Now we can do it again with multiply we could say you know 10 times five should be 50 after it's calculated.
So let's do that again let's run this test 10 times five should result in 50.
Launching the test.
Testing and success.
It worked awesome.
So we are now successfully testing our calculation.
And now let's make sure that when we actually test our values that it actually clears them all out.
So let's type phunk test maybe clear K when called Clear's values.
So let's just go ahead and go through this process again let's insert all of this.
Or how about let's just insert one value can let's say we changed our mind and we called Set clear values.
Now the things that should be cleared according to the function value should be set back to zero even
though it was set right here.
So let's make sure that it is X X s.t. assert equal and some value a should now be 0.
K x TC assert equal value B should also be zero.
And our value entery array should be empty.
There should be nothing in it.
So go ahead and type LCT assert equal some value and tree array.
And it should be an empty array.
So let's go ahead and let's try that out.
Of course it's going to return a string.
We use that later to basically set the label of the calculator to be a string.
Now we're just going to go ahead and do an underscore equals and then go ahead and run this test so
build it and then click the little diamond there it'll go ahead and run and then we'll see if it passed.
Here we go testing and look at that.
It passes so our data model is clean.
We have run tests to check the initialization to check the setting of values the modifying of values
as well as some of our functions that run from our model.
Very very cool stuff.
Let's head over to the next video where we're going to start testing out some of our features and functions
and writing tests for our view controller.
I'll see you there.