tea-time/presentation/06_context.slides

131 lines
2.0 KiB
Plaintext
Raw Permalink Normal View History

2012-03-13 17:29:42 +00:00
!SLIDE
# A little semantics game...
!SLIDE even-larger
``` coffeescript
describe 'Cat', ->
describe '#meow', ->
describe 'hungry', ->
# cat codes
describe 'going to the vet', ->
# moar cat codes
```
!SLIDE
# This works, but it can be clearer
2012-03-24 14:20:56 +00:00
!SLIDE even-larger
2012-03-13 17:29:42 +00:00
``` ruby
describe Cat do
describe '#meow' do
describe 'hungry' do
# cat codes
end
describe 'going to the vet' do
# moar cat codes
end
end
end
```
!SLIDE
# `context`
!SLIDE
# Description of different states for a test
!SLIDE even-larger
``` ruby
alias :context :describe
```
2012-03-24 14:20:56 +00:00
!SLIDE even-larger
2012-03-13 17:29:42 +00:00
``` ruby
describe Cat do
let(:cat) { described_class.new }
2012-03-24 14:20:56 +00:00
# save describe for
# things or behaviors...
2012-03-13 17:29:42 +00:00
describe '#meow' do
subject { cat.meow }
# use context to describe states
context 'hungry' do
# cat codes
end
context 'going to the vet' do
# moar cat codes
end
end
end
```
!SLIDE
# Jasmine doesn't have `context`
!SLIDE
# However...
!SLIDE even-larger
``` coffeescript
this.context = this.describe
```
!SLIDE even-larger
``` coffeescript
this.context = this.describe
describe 'Cat', ->
describe '#meow', ->
context 'hungry', ->
# cat codes
context 'going to the vet', ->
# moar cat codes
```
!SLIDE larger
``` coffeescript
this.context = this.describe
describe 'Cat', ->
describe '#meow', ->
beforeEach ->
@cat = new Cat()
context 'hungry', ->
it 'should be a mournful meow', ->
@cat.state = -> Cat.HUNGRY
expect(@cat.meow()).toEqual("meeeyaow")
context 'going to the vet', ->
it 'should be an evil meow', ->
@cat.state = -> Cat.VET_PSYCHIC
expect(@cat.meow()).toEqual("raowwww")
```
!SLIDE even-larger
``` coffeescript
2012-03-24 14:20:56 +00:00
class @Cat
2012-03-13 17:29:42 +00:00
@HUNGRY = 'hungry'
@VET_PSYCHIC = 'vet psychic'
meow: ->
switch this.state()
when Cat.HUNGRY
"meeeyaow"
when Cat.VET_PSYCHIC
"raowwww"
```
!SLIDE even-larger
```
2 spec, 0 failures
```