The rebels have boarded our death square and are heading for our data banks. A platoon of storm troopers has been sent to stop them, but we need to know the second they go offline so we can switch to a backup. Let's do some test-driven development.
-
Open up our data banks. Navigate into
turret-apiand runseed.jsandserver.js— don't forget to install thosenpmmodules. -
Navigate into
imperial-starfleetand start up our front end. -
Run
ng testinsideimperial-starfleet. Nice - we're passing two tests! But there's one failing test onInfoWindow. Let's make it pass. -
That failing test is complaining that there is
No provider for Http. WhatModulecould we import toinfo-window.component.spec.ts? Hint: It's toward the top ofapp.module.ts. Don't forget to add it to animportsarray below thedeclarationsarray inTestBed.configureTestingModule. Save the file and check theng testbrowser output. -
We're still failing! We need to import one more thing. It's called
RouterTestingModule, and it comes from the@angular/router/testingpackage. Don't forget to add it to theimportsarray, too. Save the file and check theng testbrowser output one more time. -
Create a new test below the "should be created" one and call it "should have a
findTurret()function to get data from the data banks." Fail this test the old-fashioned way by making it testexpect(true).toEqual(false);. -
Now, let's actually test something. We're going to make a lot of changes to our Death Square Dashboard™, and we need to make sure that
findTurret()is still a working function. Replace ourexpect(true)...expectation withexpect(typeof(component.findTurret)).toBe('function');. -
We want to know the second that our turret damage dataBanks go down, so let's create a test called it "should return a turret object", and create a
turretNumbervariable with a value of4. -
Call the
component.findTurret()function withturretNumberas an argument. Then.subscribe()to it, andconsole.log()theresponsecoming back from the API. Make sure you parse it into.json()first. You should see thisconsole.log()in the Terminal. -
Save this
responseJSONinto a variable, and check the following:
a) Check that the typeOf responseJSON is an 'object' (see step 7 for more syntax)
b) Check that responseJSON.turretNumber is a 'number'
c) Check that responseJSON.damage is a 'string'--woo hoo, a failure! Now, check if it's a 'number'
- Check your tests in Chrome one more time. Once they're all passing, we know one more thing is safe from the rebels.
- We shouldn't just check
turretNumber4 all the time. How could you useMath.ceil()andMath.rand()to test a turret at random?
