Slide 1

Slide 1 text

confidence.js

Slide 2

Slide 2 text

"I don't test-drive my JavaScript because it changes too often" - Anonymous

Slide 3

Slide 3 text

Why does it change more often?

Slide 4

Slide 4 text

Because it has more reasons to!

Slide 5

Slide 5 text

$(function(){ });

Slide 6

Slide 6 text

$(function(){ var $button = $('.submit-button'); });

Slide 7

Slide 7 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ }); });

Slide 8

Slide 8 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); }); });

Slide 9

Slide 9 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ }); }); });

Slide 10

Slide 10 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val() }); }); });

Slide 11

Slide 11 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ } }); }); });

Slide 12

Slide 12 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); });

Slide 13

Slide 13 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); });

Slide 14

Slide 14 text

simple!

Slide 15

Slide 15 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); });

Slide 16

Slide 16 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); }); anonymous, not reusable

Slide 17

Slide 17 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); }); anonymous, not reusable handle page event

Slide 18

Slide 18 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); }); anonymous, not reusable handle page event user event handling

Slide 19

Slide 19 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); }); anonymous, not reusable handle page event user event handling sends a network request

Slide 20

Slide 20 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); }); anonymous, not reusable handle page event user event handling sends a network request form processing

Slide 21

Slide 21 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); }); anonymous, not reusable handle page event user event handling sends a network request form processing network event handling

Slide 22

Slide 22 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); }); anonymous, not reusable handle page event user event handling sends a network request form processing network event handling HTML templating

Slide 23

Slide 23 text

$(function(){ var $button = $('.submit-button'); $button.live('click', function(){ var $input = $('input.secret-code'); $.ajax({ data: $input.val(), success: function(data){ $('.messages').append( '
Success! '+data.result+'
' ); } }); }); }); anonymous, not reusable handle page event user event handling sends a network request form processing network event handling HTML templating

Slide 24

Slide 24 text

SRP, or...

Slide 25

Slide 25 text

SR—WHEE!

Slide 26

Slide 26 text

information density

Slide 27

Slide 27 text

can we test that code as-is?

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

describe("~ clicking a button ", function() { var $button, $input, $messages; beforeEach(function(){ $button = affix('.submit-button'); $input = affix('input.secret-code').val('ZOMG!'); $messages = affix('.messages'); spyOn($, "ajax"); $button.trigger('click'); }); it("transmits secret code", function() { expect($.ajax).toHaveBeenCalledWith({ data: 'ZOMG!', success: jasmine.any(Function) }); }); describe("~ AJAX success", function() { beforeEach(function() { $.ajax.mostRecentCall.args[0].success({ result: "Panda!" }); }); it("appends text", function() { expect($messages).toHaveHtml( '
Success! Panda!
'); }); }); });

Slide 30

Slide 30 text

describe("~ clicking a button ", function() { var $button, $input, $messages; beforeEach(function(){ $button = affix('.submit-button'); $input = affix('input.secret-code'). val('ZOMG!'); $messages = affix('.messages'); spyOn($, "ajax"); $button.trigger('click'); }); ...

Slide 31

Slide 31 text

describe("~ clicking a button ", function() { beforeEach(function(){ ... $button.trigger('click'); }); it("transmits secret code", function() { expect($.ajax).toHaveBeenCalledWith({ data: 'ZOMG!', success: jasmine.any(Function) }); }); ...

Slide 32

Slide 32 text

describe("~ clicking a button ", function() { beforeEach(function(){ ... $button.trigger('click'); }); ... describe("~ AJAX success", function() { beforeEach(function() { $.ajax.mostRecentCall.args[0].success({ result: "Panda!" }); }); it("appends text", function() { expect($messages).toHaveHtml( '
Success! Panda!
'); }); }); });

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Testing vs. TDD

Slide 35

Slide 35 text

let's talk feedback

Slide 36

Slide 36 text

What can a full-stack test tell us?

Slide 37

Slide 37 text

1. A feature is implemented

Slide 38

Slide 38 text

2. The application seems to be working

Slide 39

Slide 39 text

3. We didn't just break everything

Slide 40

Slide 40 text

What can a unit test tell us?

Slide 41

Slide 41 text

1. How the unit behaves

Slide 42

Slide 42 text

2. How the unit depends on other units

Slide 43

Slide 43 text

3. How great/awful the unit's API is

Slide 44

Slide 44 text

TDD isn't about catching bugs

Slide 45

Slide 45 text

TDD isn't about preventing bugs

Slide 46

Slide 46 text

TDD isn't about testing

Slide 47

Slide 47 text

TDD is about thinking harder

Slide 48

Slide 48 text

...we can't look at testing mechanistically. Unit testing does not improve quality just by catching errors at the unit level... The truth is more subtle than that. Quality is a function of thought and reflection - precise thought and reflection. That’s the magic. Techniques which reinforce that discipline invariably increase quality. -Michael Feathers " "

Slide 49

Slide 49 text

TDD is about responding to painful designs

Slide 50

Slide 50 text

TDD is about discovering what we need

Slide 51

Slide 51 text

TDD is not the only way

Slide 52

Slide 52 text

let's try again

Slide 53

Slide 53 text

our serious app has invoices

Slide 54

Slide 54 text

describe("app.models.Invoice", function() { });

Slide 55

Slide 55 text

describe("app.models.Invoice", function() { var subject; });

Slide 56

Slide 56 text

describe("app.models.Invoice", function() { var subject; beforeEach(function() { }); });

Slide 57

Slide 57 text

describe("app.models.Invoice", function() { var subject; beforeEach(function() { subject = app.models.Invoice(); }); });

Slide 58

Slide 58 text

describe("app.models.Invoice", function() { var subject; beforeEach(function() { subject = app.models.Invoice({ price: 192, quantity: 30}); }); });

Slide 59

Slide 59 text

describe("app.models.Invoice", function() { var subject; beforeEach(function() { subject = app.models.Invoice({ price: 192, quantity: 30}); }); describe("#total", function() { }); });

Slide 60

Slide 60 text

describe("app.models.Invoice", function() { var subject; beforeEach(function() { subject = app.models.Invoice({ price: 192, quantity: 30}); }); describe("#total", function() { it("is price x quantity", function() { }); }); });

Slide 61

Slide 61 text

describe("app.models.Invoice", function() { var subject; beforeEach(function() { subject = app.models.Invoice({ price: 192, quantity: 30}); }); describe("#total", function() { it("is price x quantity", function() { expect(subject.total()).toEqual(5760); }); }); });

Slide 62

Slide 62 text

app.models.Invoice = function(attrs) { return { total: function() { return attrs.price * attrs.quantity; } }; };

Slide 63

Slide 63 text

Now, in Co eeVision

Slide 64

Slide 64 text

describe "app.models.Invoice", -> beforeEach -> @subject = app.models.Invoice price: 192 quantity: 30 describe "#total", -> it "is price x quantity", -> expect(@subject.total()).toEqual(5760)

Slide 65

Slide 65 text

describe("app.models.Invoice", function() { var subject; beforeEach(function() { subject = app.models.Invoice({ price: 192, quantity: 30}); }); describe("#total", function() { it("is price x quantity", function() { expect(subject.total()).toEqual(5760); }); }); });

Slide 66

Slide 66 text

now, in Given-When-Then w/ jasmine-given

Slide 67

Slide 67 text

describe "app.models.Invoice", -> Given -> @subject = app.models.Invoice price: 192 quantity: 30 describe "#total", -> Then -> @subject.total() == 192 * 30

Slide 68

Slide 68 text

describe "app.models.Invoice", -> beforeEach -> @subject = app.models.Invoice price: 192 quantity: 30 describe "#total", -> it "is price x quantity", -> expect(@subject.total()).toEqual(5760)

Slide 69

Slide 69 text

let's inflict some pain

Slide 70

Slide 70 text

formatting dollars

Slide 71

Slide 71 text

describe "app.models.Invoice", -> Given -> @subject = app.models.Invoice price: 192 quantity: 30 describe "#total", -> Then -> @subject.total() == 192 * 30 describe "#formattedTotal", -> When -> @result = @subject.formattedTotal() Then -> @result == "$57.60"

Slide 72

Slide 72 text

app.models.Invoice = (attrs) -> self = {} self.total = -> attrs.price * attrs.quantity self

Slide 73

Slide 73 text

app.models.Invoice = (attrs) -> self = {} self.total = -> attrs.price * attrs.quantity self.formattedTotal = -> "$#{self.total() / 100.0}" self

Slide 74

Slide 74 text

$57.6

Slide 75

Slide 75 text

hrm.

Slide 76

Slide 76 text

(punting on) formatting dollars

Slide 77

Slide 77 text

Defer, Defer, Defer!

Slide 78

Slide 78 text

describe "app.models.Invoice", -> Given -> @subject = app.models.Invoice price: 192 quantity: 30 describe "#total", -> Then -> @subject.total() == 192 * 30 describe "#formattedTotal", -> When -> @result = @subject.formattedTotal() Then -> @result == "$57.60"

Slide 79

Slide 79 text

describe "app.models.Invoice", -> Given -> @subject = app.models.Invoice price: 192 quantity: 30 describe "#total", -> Then -> @subject.total() == 192 * 30 describe "#formattedTotal", -> Given -> spyOn(app.format, "DollarizesCents").andReturn dollarize: -> "$57.60" When -> @result = @subject.formattedTotal() Then -> @result == "$57.60"

Slide 80

Slide 80 text

spyOn(app.format, "DollarizesCents").andReturn dollarize: -> "$57.60" #1. Store the real app.format.DollarizesCents #2. Set app.format.DollarizesCents to a fake function (called a spy). #3. Tell that spy function to return some object with a dollarize method that returns the test data "$57.60" #4. After the spec runs, replace the original app.format.DollarizesCents function

Slide 81

Slide 81 text

spyOn(obj, "methodName")

Slide 82

Slide 82 text

jasmine.createSpy("blah")

Slide 83

Slide 83 text

app.models.Invoice = (attrs) -> self = {} self.total = -> attrs.price * attrs.quantity self.formattedTotal = -> "$#{self.total() / 100.0}" self

Slide 84

Slide 84 text

app.models.Invoice = (attrs) -> self = {} self.total = -> attrs.price * attrs.quantity self.formattedTotal = -> app.format.DollarizesCents(). dollarize(self.total()) self

Slide 85

Slide 85 text

fake it 'til you make it

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

app.models.Invoice = (attrs) -> self = {} self.total = -> attrs.price * attrs.quantity self.formattedTotal = -> app.format.DollarizesCents(). dollarize(9182128912891) self

Slide 88

Slide 88 text

describe "app.models.Invoice", -> Given -> @subject = app.models.Invoice price: 192 quantity: 30 describe "#total", -> Then -> @subject.total() == 192 * 30 describe "#formattedTotal", -> Given -> spyOn(app.format, "DollarizesCents").andReturn dollarize: jasmine.createSpy().when(5760).thenReturn("$57.60") When -> @result = @subject.formattedTotal() Then -> @result == "$57.60"

Slide 89

Slide 89 text

app.models.Invoice = (attrs) -> self = {} self.total = -> attrs.price * attrs.quantity self.formattedTotal = -> app.format.DollarizesCents(). dollarize(9182128912891) self

Slide 90

Slide 90 text

app.models.Invoice = (attrs) -> self = {} self.total = -> attrs.price * attrs.quantity self.formattedTotal = -> app.format.DollarizesCents(). dollarize(self.total()) self

Slide 91

Slide 91 text

when().thenReturn() & other spy gadgets are in jasmine-stealth

Slide 92

Slide 92 text

(actually) formatting dollars

Slide 93

Slide 93 text

describe "app.format.DollarizesCents", -> Given -> @subject = app.format.DollarizesCents() describe "#dollarize", -> context "0 cents", -> Then -> @subject.dollarize(0) == "$0.00"

Slide 94

Slide 94 text

app.format.DollarizesCents = -> dollarize: (cents) -> "$0.00"

Slide 95

Slide 95 text

describe "app.format.DollarizesCents", -> Given -> @subject = app.format.DollarizesCents() describe "#dollarize", -> context "0 cents", -> Then -> @subject.dollarize(0) == "$0.00" context "50 cents", -> Then -> @subject.dollarize(50) == "$0.50"

Slide 96

Slide 96 text

Crap.

Slide 97

Slide 97 text

app.format.DollarizesCents = -> dollarize: (cents) -> dollars = cents / 100.0 decimal = if dollars % 1 == 0 then "." else "" trailingZero = if cents % 10 == 0 then "0" else "" trailingDoubleZero = if cents % 100 == 0 then "0" else "" "$#{dollars}#{decimal}#{trailingZero}#{trailingDoubleZero}"

Slide 98

Slide 98 text

Yuck!

Slide 99

Slide 99 text

app.format.DollarizesCents = -> dollarize: (cents) -> s = "$" s += (dollars = cents / 100.0) s += "." if dollars % 1 == 0 s += "0" if cents % 10 == 0 s += "0" if cents % 100 == 0 s

Slide 100

Slide 100 text

Meh.

Slide 101

Slide 101 text

Let's lean into it.

Slide 102

Slide 102 text

describe "app.format.DollarizesCents", -> Given -> @subject = app.format.DollarizesCents() describe "#dollarize", -> context "0 cents", -> Then -> @subject.dollarize(0) == "$0.00" context "50 cents", -> Then -> @subject.dollarize(50) == "$0.50" context "1,841,482 cents", -> Then -> @subject.dollarize(1841482) == "$18,414.82"

Slide 103

Slide 103 text

app.format.DollarizesCents = -> dollarize: (cents) -> amount = cents / 100.0 s = "$" s += @commasFor(amount) s += "0" if cents % 10 == 0 s += "0" if cents % 100 == 0 s #private commasFor: (amount) -> [dollars, cents] = amount.toString().split(".") s = "" while dollars.length > 3 s = ",#{dollars.slice(-3)}#{s}" dollars = dollars.substring(0, dollars.length - 3) "#{dollars}#{s}.#{cents or ""}"

Slide 104

Slide 104 text

I am now embarrassed.

Slide 105

Slide 105 text

describe "app.format.DollarizesCents", -> Given -> @subject = app.format.DollarizesCents() describe "#dollarize", -> context "0 cents", -> Then -> @subject.dollarize(0) == "$0.00" context "50 cents", -> Then -> @subject.dollarize(50) == "$0.50" context "1,841,482 cents", -> Then -> @subject.dollarize(1841482) == "$18,414.82" context "213,981,400 cents", -> Then -> @subject.dollarize(213981400) == "$2,139,814.00"

Slide 106

Slide 106 text

app.format.DollarizesCents = -> dollarize: (cents) -> amount = cents / 100.0 s = "$" s += @commasFor(amount) s += "0" if cents % 10 == 0 s += "0" if cents % 100 == 0 s #private commasFor: (amount) -> [dollars, cents] = amount.toString().split(".") s = "" while dollars.length > 3 s = ",#{dollars.slice(-3)}#{s}" dollars = dollars.substring(0, dollars.length - 3) "#{dollars}#{s}.#{cents or ""}"

Slide 107

Slide 107 text

commasFor is doing too much

Slide 108

Slide 108 text

app.format.DollarizesCents = -> dollarize: (cents) -> amount = cents / 100.0 s = "$" s += @commasFor(amount) s += "0" if cents % 10 == 0 s += "0" if cents % 100 == 0 s #private commasFor: (amount) -> [dollars, cents] = amount.toString().split(".") s = "" while dollars.length > 3 s = ",#{dollars.slice(-3)}#{s}" dollars = dollars.substring(0, dollars.length - 3) "#{dollars}#{s}.#{cents or ""}"

Slide 109

Slide 109 text

app.format.DollarizesCents = -> dollarize: (pennies) -> amount = pennies / 100.0 [dollars, cents] = amount.toString().split(".") s = "$" s += @commasFor(dollars) s += ".#{cents or ""}" s += "0" if pennies % 10 == 0 s += "0" if pennies % 100 == 0 s #private commasFor: (dollars) -> s = "" while dollars.length > 3 s = ",#{dollars.slice(-3)}#{s}" dollars = dollars.substring(0, dollars.length - 3) "#{dollars}#{s}

Slide 110

Slide 110 text

app.format.DollarizesCents = -> dollarize: (pennies) -> amount = pennies / 100.0 [dollars, cents] = amount.toString().split(".") s = "$" s += @commasFor(dollars) s += "." s += @paddingFor(cents) s #private commasFor: (dollars) -> s = "" while dollars.length > 3 s = ",#{dollars.slice(-3)}#{s}" dollars = dollars.substring(0, dollars.length - 3) "#{dollars}#{s}" paddingFor: (cents = "") -> while cents.length < 2 cents += "0" cents

Slide 111

Slide 111 text

app.format.DollarizesCents = -> dollarize: (pennies) -> amount = pennies / 100.0 [dollars, cents] = amount.toString().split(".") "$#{@commasFor(dollars)}.#{@paddingFor(cents)}" #private commasFor: (dollars) -> s = "" while dollars.length > 3 s = ",#{dollars.slice(-3)}#{s}" dollars = dollars.substring(0, dollars.length - 3) "#{dollars}#{s}" paddingFor: (cents = "") -> while cents.length < 2 cents += "0" cents

Slide 112

Slide 112 text

app.format.DollarizesCents = -> dollarize: (pennies) -> amount = (pennies / 100.0).toFixed(2) [dollars, cents] = amount.toString().split(".") "$#{@commasFor(dollars)}.#{cents}" #private commasFor: (dollars) -> s = "" while dollars.length > 3 s = ",#{dollars.slice(-3)}#{s}" dollars = dollars.substring(0, dollars.length - 3) "#{dollars}#{s}"

Slide 113

Slide 113 text

app.format.DollarizesCents = -> dollarize: (pennies) -> amount = (pennies / 100.0).toFixed(2) [dollars, cents] = amount.toString().split(".") "$#{@commasFor(dollars)}.#{cents}" #private commasFor: (dollars) -> dollars.replace /(\d)(?=(\d{3})+$)/g, "\$1,"

Slide 114

Slide 114 text

what about the DOM?

Slide 115

Slide 115 text

I want my asp/jsp/erb!

Slide 116

Slide 116 text

No you don't.

Slide 117

Slide 117 text

I want to load HTML fixture files!

Slide 118

Slide 118 text

No you don't.

Slide 119

Slide 119 text

describe "app.views.Invoice", -> Given -> @subject = app.views.Invoice()

Slide 120

Slide 120 text

describe "app.views.Invoice", -> Given -> @subject = app.views.Invoice() describe "#render", ->

Slide 121

Slide 121 text

describe "app.views.Invoice", -> Given -> @subject = app.views.Invoice() describe "#render", -> Given -> @$el = $('
'). appendTo('body')

Slide 122

Slide 122 text

describe "app.views.Invoice", -> Given -> @subject = app.views.Invoice() describe "#render", -> Given -> @$el = $('
'). appendTo('body') When -> @subject.render(@$el)

Slide 123

Slide 123 text

describe "app.views.Invoice", -> Given -> @subject = app.views.Invoice() describe "#render", -> Given -> @$el = $('
'). appendTo('body') When -> @subject.render(@$el) Then -> expect(@$el).toContain('.total')

Slide 124

Slide 124 text

app.views.Invoice = () -> render: ($el) -> $el.append("
")

Slide 125

Slide 125 text

test pollution

Slide 126

Slide 126 text

describe "app.views.Invoice", -> Given -> @model = app.models.Invoice() Given -> @subject = app.views.Invoice model: @model describe "#render", -> Given -> @$el = $('
'). appendTo('body') When -> @subject.render(@$el) Then -> expect(@$el).toContain('.total')

Slide 127

Slide 127 text

describe "app.views.Invoice", -> Given -> @model = app.models.Invoice() Given -> @subject = app.views.Invoice model: @model describe "#render", -> Given -> @$el = $('
'). appendTo('body') afterEach -> @$el.remove() When -> @subject.render(@$el) Then -> expect(@$el).toContain('.total')

Slide 128

Slide 128 text

describe "app.views.Invoice", -> Given -> @model = app.models.Invoice() Given -> @subject = app.views.Invoice model: @model describe "#render", -> Given -> @$el = affix('div') When -> @subject.render(@$el) Then -> expect(@$el).toContain('.total')

Slide 129

Slide 129 text

jasmine-fixture a x()

Slide 130

Slide 130 text

it's jQuery in reverse!

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

$button = affix('.button')

Slide 133

Slide 133 text

$button = affix('.button') #=>

Slide 134

Slide 134 text

$button = affix('.button') #=>
$button.affix('input#firstName[value="Joe"]')

Slide 135

Slide 135 text

$button = affix('.button') #=>
$button.affix('input#firstName[value="Joe"]') #=>

Slide 136

Slide 136 text

$button = affix('.button') #=>
$button.affix('input#firstName[value="Joe"]') #=>
affix('pre code div.example')

Slide 137

Slide 137 text

$button = affix('.button') #=>
$button.affix('input#firstName[value="Joe"]') #=>
affix('pre code div.example') #=>

Slide 138

Slide 138 text

$button = affix('.button') #=>
$button.affix('input#firstName[value="Joe"]') #=>
affix('pre code div.example') #=>
# And it deletes affixed elements afterEach spec!

Slide 139

Slide 139 text

Now, formatted price

Slide 140

Slide 140 text

describe "app.views.Invoice", -> Given -> @subject = app.views.Invoice() describe "#render", -> Given -> @$el = affix('div') When -> @subject.render(@$el) Then -> expect(@$el).toContain('.total')

Slide 141

Slide 141 text

describe "app.views.Invoice", -> Given -> @model = app.models.Invoice price: 38 quantity: 2 Given -> @subject = app.views.Invoice model: @model describe "#render", -> Given -> @$el = affix('div') When -> @subject.render(@$el) Then -> expect(@$el.find('.total')). toHaveText("$0.76")

Slide 142

Slide 142 text

app.views.Invoice = (config) -> render: ($el) -> $el.append "
"+ config.model.formattedTotal()+ "
"

Slide 143

Slide 143 text

spec leakage

Slide 144

Slide 144 text

the view spec knows how the model's formattedTotal works

Slide 145

Slide 145 text

that's not very DRY

Slide 146

Slide 146 text

collaboration > implementation

Slide 147

Slide 147 text

describe "app.views.Invoice", -> Given -> @model = app.models.Invoice price: 38 quantity: 2 Given -> @subject = app.views.Invoice model: @model describe "#render", -> Given -> @$el = affix('div') When -> @subject.render(@$el) Then -> expect(@$el).toContain('.total') Then -> expect(@$el.find('.total')). toHaveText("$0.76")

Slide 148

Slide 148 text

describe "app.views.Invoice", -> Given -> @model = {} Given -> @subject = app.views.Invoice model: @model describe "#render", -> Given -> @$el = affix('div') When -> @subject.render(@$el) Then -> expect(@$el).toContain('.total') Then -> expect(@$el.find('.total')). toHaveText("$0.76")

Slide 149

Slide 149 text

describe "app.views.Invoice", -> Given -> @model = {} Given -> @subject = app.views.Invoice model: @model describe "#render", -> Given -> @model.formattedTotal = jasmine.createSpy().andReturn("$foo") Given -> @$el = affix('div') When -> @subject.render(@$el) Then -> expect(@$el).toContain('.total') Then -> expect(@$el.find('.total')). toHaveText("$foo")

Slide 150

Slide 150 text

TIL

Slide 151

Slide 151 text

If you're not open to changing your code's design, TDD won't help.

Slide 152

Slide 152 text

TDD helps us to: - think hard - respond to pain - keep moving forward (tests are a side e ect)

Slide 153

Slide 153 text

When a solution isn't obvious, defer to a new object and fake it 'til you make it.

Slide 154

Slide 154 text

Shared factories & fixtures lead to a Tragedy of the Commons

Slide 155

Slide 155 text

Isolate subjects from collaborators because it'll hurt so good.

Slide 156

Slide 156 text

http://test-double.com @searls http://github.com/searls http://tryjasmine.com