-
-
Save rubygeek/4111823 to your computer and use it in GitHub Desktop.
| class Breadcrumb | |
| Crumb = Struct.new(:name, :link) | |
| attr_reader :crumbs | |
| def initialize(name = "Home", link = "/") | |
| @crumbs = [] | |
| self.add(name, link) | |
| end | |
| def add(name, link) | |
| @crumbs << Crumb.new(name,link) | |
| end | |
| end | |
| ------ and tests -------- | |
| describe Breadcrumb do | |
| describe '#new' do | |
| it 'sets default crumb of Home /' do | |
| @breadcrumb = Breadcrumb.new | |
| @breadcrumb.should have(1).crumb | |
| @breadcrumb.crumbs.first.name.should == 'Home' | |
| @breadcrumb.crumbs.first.link.should == '/' | |
| end | |
| it 'sets initial crumb of Main /main' do | |
| @breadcrumb = Breadcrumb.new('Main', '/main') | |
| @breadcrumb.should have(1).crumb | |
| @breadcrumb.crumbs.first.name.should == 'Main' | |
| @breadcrumb.crumbs.first.link.should == '/main' | |
| end | |
| end | |
| describe '#add' do | |
| it 'a second crumb' do | |
| @breadcrumb = Breadcrumb.new | |
| @breadcrumb.add('Products', '/products') | |
| @breadcrumb.should have(2).crumbs | |
| end | |
| end | |
| --------- | |
| output of spec doc | |
| Breadcrumb | |
| #new | |
| sets default crumb of Home / | |
| sets initial crumb of Main /main | |
| #add | |
| a second crumb |
I, too, have a couple of nits, specifically style nits regarding the specs (the class under test looks legit to me):
- I'd use a couple of inner
describeblocks. The top two are about.newand the last is about.add. If you put a leading.or#in adescribecall, RSpec will concat it to the name of the class under test without white space in the output. - After breaking them up into two groups like that, I'd probably just do away with the
beforeand handle that in each test individually. If you had more examples that shared needed setup within my proposeddescribeblocks, I'd break them up with acontexteach and put thebeforein there. - You'll have to rejigger the strings you feed into
itat this point if the output is to make any sense. Relatedly, I always try to avoid "should" and, instead, pass things like "adds a crumb" rather than "should add a crumb".
If you're interested, I wrote a blog post about some of my thinking behind the above points. Also, I'm happy to be disagreed with on this stuff; I'm still sorting out how to write maintainable specs, so I welcome an opportunity to learn how other folks think about it.
good point... I think i had it as reader initially but when i an error i change trying to figure out what was going on ... turned out i had def initializer instead of def initialize (doh) and didn't change it back!
Thanks Ben, good suggestions... I forgot about describe "#method.." i've used that before. Thanks again!!
hmm i stil not happy with the names of the tests for #new ...
Small nit: I'd make the
crumbsaccessor a reader.addis the only thing that should mutate your crumb collection.