Upload
stephen-pond
View
701
Download
2
Tags:
Embed Size (px)
Citation preview
TripCase Unit Testing with JasminePresented by Steve Pond @stephenpond
Tuesday, December 11, 12
Overview
What to Unit Test?
Test Driven Development
Walkthrough of Jasmine Unit Testing and Tools
Walkthrough of JSCover - coverage testing
Q & A
Tuesday, December 11, 12
Unit TestingRepeatable: You can rerun the same test as many times as you want.
Consistent: Every time you run it, you get the same result. (for example: Using threads can produce an inconsistent result)
In Memory: It has no “hard” dependencies on anything not in memory (such as file system, databases, network)
Fast: It should take less than half a second to run a unit test.
Checking one single concern or “use case” in the system: (More than one can make it harder to understand what or where the problem is when the problem arises.)
Tuesday, December 11, 12
Integration TestingUse system dependent values that change dynamically (such as DateTime.Now, or Environment.MachineName)
Create objects of which it has little control (such as threads, random number generators)
Reach out to external systems or local machine dependencies (from calling web services to using local configuration files)
Test multiple things in the course of one test case (from database integrity, to configurations, to protocols, to system logic, in one go).
Tuesday, December 11, 12
What to Unit Test?
Tuesday, December 11, 12
Framework implements multiple libraries
Inner-team dialogues are generally integration related
Deadline Driven. Zero time.
GreenField - BrownField
TripCase UT Barriers
Tuesday, December 11, 12
ProcedureDetermining what is unit-testable
Tuesday, December 11, 12
ReflexiveDoes its own thing... (extension to our app’s core logic)
Tuesday, December 11, 12
AlgorithmicOur apps consciousness, makes decisions, etc...
Tuesday, December 11, 12
TripCase Analysis
Core
Router
Views
Controller
Workflows
Model/Collection
Tuesday, December 11, 12
Core
Heartbeat
Sets up Require config
app.js
Initializes app modules
Tuesday, December 11, 12
Core - Reflexive
Mainly just initialization
Launching the App
Tuesday, December 11, 12
Router
History Stack
Navigation API
Map hash change to mediator events & vice versa
Tuesday, December 11, 12
Router - Reflexive
Mainly just mapping hash changes to mediator events
API, and history stack, handed to us
Tuesday, December 11, 12
Views
Views present model data and respond to client interactions
Ideally, views just render and invoke actions on the model
Tuesday, December 11, 12
Views - Reflexive
Views merely couple the model and the client interaction
Up for debate: some success/fail scenarios sometimes handled in view? Sounds Algorithmic.
Use discretion on Unit Testing
Tuesday, December 11, 12
Controllers
Module/Feature level, initializes the workflow or view
Workflow handles app state change, swaps views
Controller listens to mediator events for various action
Listens to view/workflow level events
Tuesday, December 11, 12
Controller - Reflexive
Workflows should be kept lean (refer to SOLID principle) and stick to single responsibility rule
Controllers mainly just mapping the mediator to workflow or view initializations
In practice, I have seen a lot of app logic handled in workflows. Unit Test with discretion.
Tuesday, December 11, 12
Models
Sync, fetch, and Save
Special parsing
Special validation
Special helpers
Special URL and payload
Tuesday, December 11, 12
Models - Algorithmic
Models/ Collections - clear choice for unit test candidates
We provide a lot of logic for parsing and helpers here
Tuesday, December 11, 12
Next Step: Draft our spec
Analyze a story
Pseudo Code
Tuesday, December 11, 12
Analyze Story: Share Itinerary
Contacts Collection
Share contacts helper should return all contacts that are shared
Contacts model
Should properly construct a URL for syncing
Should properly construct a payload for fetching
Should properly construct a payload for saving
Should properly validate user Input
Should properly set attributes for a given response
Tuesday, December 11, 12
Test-Driven Development
Tuesday, December 11, 12
Ken Beckwho is credited with having developed or 'rediscovered' the technique, stated in
TDD encourages simple designs and inspires
confidence.
Tuesday, December 11, 12
A Simple TDD Workflow 1.Write test stubs based on business requirements for a new feature 2.Write minimal code in Spec to PASS the unit test 3.Tweak code to pass the FUNCTIONAL test 4.Go back and tweak the unit test with new code and Together until SUCCESS
Tuesday, December 11, 12
Group Activity: Analyze a Story
Given the story of a Search Hotel Module, identify the unit-testable assertions
Apply it to a minimal Jasmine Spec Template
Tuesday, December 11, 12
Jasmine Basics
Tuesday, December 11, 12
Jasmine Basics - Blocks
Tuesday, December 11, 12
Jasmine Basics - Setup/Teardown
Tuesday, December 11, 12
Jasmine Basics - Spies
Tuesday, December 11, 12
Jasmine Basics - Sinon(not really unit-testing, but useful for integration testing)
Tuesday, December 11, 12
Walkthrough
Tuesday, December 11, 12