Unit Testing Javascript Javascript Architecture Why QUnit? Why Mockjax? With QUnit and Mockjax Unit Testing Matters Let's Look at Code There are many options Seems to be the most robust out there Tools are great - But is your Javascript Architected for Unit Testing? Continuous Integration Running QUnit Tests from Your Build Server Nick Schweitzer QUnit Developed and Used by jQuery Team Can Be Run Outside of Browser with PhantomJS Allows for Integration with Build Servers! Built-In Support in Resharper 6.1 (Though Still a Bit Buggy) Allows for Mocking AJAX Calls Through Interception View-Model Code Doesn't Have to Be Written Specifically to Handle Mockjax! QUnit Has Hooks To Run Functions on Test Start, Success and Failure Can be Easily Integrated with JetBrains Team City - and Possibly Others KnockoutJS MVVM Is Not Just Important Because of Binding! Could Also Use Sinon.JS - Doesn't Seem as Good for Mocking as Mockjax Why Not Backbone? Why Not a Lot of Things? Backbone Really Isn't an MVC Architecture * * You May Disagree - It's MV Something.. Maybe MVP... Maybe a Router Pattern Using MVC to Create Test Harness A Great Idea... If You Don't Use a Build Server Based on an Article from Phil Haack http://haacked.com/archive/2011/12/10/using-qunit-with-razor-layouts.aspx Why Shouldn't You Do It? Separation of Concerns in Unit Tests Why Should You Do It? @Url.Content & foreach Loops Can You Write Your Javascript Without Referencing a DIV or CLASS Name? How Do You Unit Test Something That is So Tightly Bound to the User Interface? Many Example QUnit Tests "In the Wild" Contain Test Harness Files with Mock Up DIV's that Replicate the HTML They are Bound Against Allows Javascript Code to be Truly Unit Testable Can Create Properties and Methods so Can Test Data and Behavior Independent of any Particular View Don't Have to Recreate View Objects (DIV's, etc) in Order to Test the Validity of the Data It Makes My Javascript Feel Object Oriented Which Gives me Warm Fuzzy Feelings Inside (Maybe That's Just Me) Why Shouldn't You Do It? How Will Your Build Server Run It? http://joseoncode.com/2011/08/09/running-qunit-tests-in-teamcity/ Software Architect SpiderLogic (http://www.spiderlogic.com) http://www.thecodingmonkey.net http://twitter.com/CodingMonkeyNet Backbone Has No Controllers (Routers/Views Don't Count) Backbone Certainly Isn't MVVM *Insert Religious Argument Here* Ultimately It Doesn't Matter ... Except In How You Architect Your Tests! QUnit Syntax If You've Done Unit Testing - Then There's Nothing New module() - For Creating Structure test() - Self Explanatory asyncTest() - For Creating Tests that Have AJAX Calls ok, equal, notEqual - Self Explanatory expect() - Specify Number of Assertions in Test Can Test for AJAX Calls that Timeout Can Test Different Return Codes (401, 404, etc)
More presentations by Nick Schweitzer
Guerrilla Agile - Milwaukee Agile
Nick Schweitzer on
How to Use Agile in a Company Not Receptive to Agile