mindtangle

Web 2.0 Notes: Behavior-Driven Development

This was a great talk by Gregg Pollack, who’s a big Ruby on Rails evangelist. Here, he makes his case for TDD (Test-Driven Development) in general, and a new form of it called BDD (Behavior-Driven Development) which he hearts. The live-coding was fun (it always seems fun, with Rails) but it made for some very bad note-taking. Fortunately, I found a video online with similar content, so you can watch for yourself.

This doesn’t have immediate application at Instructables, though I’ll be looking into BDD and TDD systems that have been developed for frontend code. Rough notes and links after the jump.

The Art of Testing Web Applications
Thurs 2:40 PM, Development 2003
Gregg Pollack (Rails Envy)

Links:

Gregg obviously has his bias (Rails), but this presentation is about the art of Webapp testing; approaches that are independent of platform.

Unit Testing

“A procedure used to validate that the individual components of source code are working properly”

Example given of a unit test in Java, PHP, and Ruby.

MVC: What part of the framework do we test at, say, for the submission of a blog entry? The temptation may be to write tests just to the view layer, but this means that the test will be brittle, hard to maintain, and difficult to debug. The right approach is to write unit tests at ever level of the stack.

Let’s introduce an example action: Logging into a webapp.

Do we test, say for a valid authentication? No, because this may be several steps’ worth of logic. This is like just testing the view portion of an MVC stack. If it breaks, we don’t know where. Instead, let’s break apart the action and test each part:

  • username/password received
  • user authentication
  • error on failure
  • show login on failure
  • etc.

Better yet, do test-driven development where you code to tests that you write first. Red -> Green -> Refactor, i.e. Test, Code to the Test, then Optimize.

Behavior Driven Development

Enable better TDD by focusing on behavior rather than testing. In ruby, you can use RSpec, which is a domain-specific language for BDD.

  • should be able to submit a username
  • should display an error message on failure
  • etc.

    describe "a user trying to login" do

     should ...
    
     ...
    

    end

Basically “should” is a new language feature used instead of “assert” that orders the tests in a manner more grammatically consistent with spoken English (i.e. “assert_equal a, b” becomes “a.should equal b”)

Okay, now he’s just showing off Ruby syntax, using functional aspects to refactor a dozen or so lines of code down to one, all while running his tests continually in the background, with Growl sending notifications if they fail.

Isolation is Important: The unit tests for M, V, or C should all be runnable independent of the other parts of the stack. You can do this using mocks and stubs.

[Note: at this point, I realized the slides would be more useful than my notes, so I stopped taking them...] To look up: BDD for javascript! Ruby Book Rec: The Rails Way (Obie Fernandez)

Related Posts:

Leave a Reply

alternative to diazepam buy levitra on line Metformin buying tramadol without a prescription tenuate order canadian rx ed pharmacy herbal oxazepam reviews generic cialis free sample prescription for lorazepam buy viagra now cheapest generic valium buying klipal online buy xanax online best herbal zyban