unit testing and behavior driven testing with angularjs - jesse liberty | falafelcon 2014

Post on 26-May-2015

869 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

A complete introduction to unit testing, and test-first development for AngularJS programmers. Topics include tools, specs, test runners, core testing practices and patterns, Behavior-driven development, Spys, Matchers, Stubs and MOcks and much more.

TRANSCRIPT

describe('UnitTes.ng',  func.on(){          it('works  with  AngularJS'…  }   Jesse  Liberty  

Master  Consultant  Falafel  SoFware  

jesse@falafel.com  @jesseliberty  

What  We  Will  Cover • Why  bother?      • Why  Aren't  We  Doing  It?  • Wri.ng  specs  • Matchers  •  Spies  

Getting the terms right

§  Unit Test §  Test Driven Development (TDD)

o Test First Development

§  Behavior Driven Development (BDD)

Am  I  In  The  Right  Presenta7on?

•  I  assume…  •  Comfort  with  Angular  Fundamentals  •  No  prior  experience  with  unit  tes.ng  

Ques7ons

If  you  get  lost  it  will  be  hard  to  catch  up  so  please,  please  ask  ques.ons    

Copyright  ©  2003-­‐2014  Falafel  SoFware  Inc.   5  Q&A  at  the  end    

Getting Started

§  What you already need to know o Javascript o Angular

§  What hardware you need §  What software you need §  Where to get everything

o Getting Jasmine o Finding documentation o Finding Help

jasmine.github.io  

Red, Green, Rafactor

§  Red – Your Test Should Fail §  Green – Make Your Test Pass §  Refactor – Clean up your code without breaking your test

Red, Green, Rafactor

§  Red – Your Test Should Fail §  Green – Make Your Test Work §  Refactor – Clean up your code without breaking your test

Lather  Rinse  Repeat  

Three Laws

1.  You may not write production code until you have a failing test

2.  You may not write more of a unit test than is sufficient to fail

3.  You may not write more production code than is sufficient to pass

The Prime Directive

§  Your test code should be as good and well maintained as your production code. o Succinct o Clear o Simple

§  However, do not worry about efficiency in test code

Test One Concept In Each Test

§  Pass/Fail should immediately convey what was tested and the result

F.I.R.S.T.

§  Fast §  Independent §  Repeatable §  Self-validating (boolean output) §  Timely

SEE: Setup Execute Check Expectations

Wri7ng  Your  First  Spec

Demo

Measuring your step size

Matchers

Matchers

§  toBe §  toEqual §  toBeTruthy §  toBeNull, toBeUndefined, toBeNaN §  toContain §  toMatch §  toBeLessThan §  etc.

Demo

Demo

Custom  Matchers

• How  would  we  test  that  an  expense  item  is  "reasonable?"  • Problem  #1  –  We'd  like  to  use  "ToBeReasonable"  • Problem  #2  –  How  will  the  compare  pick  out  the  amount  as  the  value  to  test?  

new  ExpenseItem(  'taxi',  'Taxi  to  airport',  89.50);  

Custom  Matchers

Demo

Making An Expense Item

§  Delete expense.js §  Add new parameters to ExpenseItem

Requirement:  Persist  expenses  and    report  number  that  were  persisted  

Demo

Ques7ons?

Contact  me:    jesse@falafel.com      @jesseliberty          

Contact  Me

•  Jesse  Liberty  •  Falafel  SoFware  •  jesse@falafel.com    • @jesseliberty  

top related