Continuing from our previous post we’re going to keep working on our Crawler and our specs to see if we can start pulling real data from our site.
The first thing I did this morning was to run my tests:
As someone totally new to TDD/BDD this is kind of an awesome feeling. I left my code for a few days and now I can come back and verify that everything still works. We can take it even further and run rspec with a documentation formatter to get some pretty printed output:
In rspec the -c flag enables color in the output. The -f flag sets a formatter and d specifies the documentation format.
Neat.
In crawler_spec.rb I’m going to add a test that checks to see if our instance has actually stored the content from our mocked web request.
I want to write a test to parse the content for keywords but I realize now that our FakeWeb request returns a string without any classes or id’s. Gotta go back and wrap it in some HTML to match our selectors. So I’m changing the mock web request to look like this:
Hello Hello Hello World!
After a lot of back and forth I finally get my test to pass. I realize along the way that there are a bunch of things I need to change. For starters having most of my words be the same count doesn’t really help me to validate that my keyword counting is working all that well. So I’m changing our FakeWeb request and the subsequent specs which test against it.
Next I need to make sure that my get_words_by_selector method is accepting a selector.
I also realize that I’d like my Array of keywords to be in desceding order so I reverse it after the initial sort.
Next I’m going to write the test to verify that we’ve received a group of words, counted them up and tossed them into an Array in descending order:
I actually wrote the test first and did everything else to make it pass. But at this point it should all be passing and we can verify that given a request with the appropriate selector we should be able to build a basic word frequency list. Yay!