RSpec for View Helpers

  • To test a view helper, you need to test the plugin in the context of a rails app
  • To verify that you can put the following code into your spec_helper.rb (or just use rspec-plugin-generator by Pat Maddox which will generate this code for you)
    begin
      require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
    rescue LoadError
      puts "You need to install rspec in your base app"
      exit
    end
  • By convention, helper examples live in spec/helpers/ then their dependencies will get magically included.   David Chelimsky notes

    All directories under spec/ are potentially magic – rspec will always
    look at spec/:directory/:filename and see if it has an example group
    type registered for whatever directory is. So if there *is* a foo
    example group type, it will be used for any spec file in spec/foos

  • If you put helpers elsewhere, then you need to declare their type (or manually include dependencies)
    describe AppletHelper, :type => :helper do
  • To send params in your test, you can just define them before calling your view helper.  For example:
    describe DogHelper do
      it "should bark" do
        params[:debug] = "foo"
        html = helper.bark
        html.should have_tag("script")
      end
    end

What do you do when some behavior depends on RAILS_ENV?

  • It’s not a good practice to switch around RAILS_ENV in the test framework
  • Better idea is use a method to check it and then stub that out
  • Here’s an outline:  gist from Zach
  • Here’s what I did: EnvChecker, code and example

Stub tip

If you stub something and it appears not to be working, check when your module, class or method is loaded or created.  Dynamic languages can be challenging.  (thanks David)

References

For more information, see

These lessons learned while developing RSpec examples for a bug fix in openlaszlo_plugin with help from Zach Moazeni, RailsBridge mentor, and David Chelmsky, on the RSpec forum.

I enjoyed Ward Cunnigham’s video tour of his office “wiki space,” which is reconfigurable by the people who work there with all desks on rollers.  At the end he describes wiki as a phillosophy rather than the technology it is today… maybe wikis need to transform into something that will enable a community to develop software.

I’m seeing some trends that I expect will meet in the middle in the next twenty years or so.  Dynamic languages are becoming fast enough that they can be widely used for practical and sophisticated programming, most notably javascript and ruby.  The facility of these programming languages to enable fast development of applications is made even faster by the move toward open source — more often we can avoid rewriting the well-understood, and can focus on the innovative.  Additionally, web application are commonly publishing APIs that edge us closer to the semantic web that folks have been talking about since before HTTP was invented.

Today we’re seeing hints of what is to come with Yahoo Pipes, Dabble DB and the recently released Wolfram Alpha — these are tools that let you piece together disparate data sources to answer a question without developing a traditional software application. Brian Boyer writes about scholarships available to coders who want to go to jouralism school.  He notes that just recently, software won a Pulitzer Prize: PolitiFact is a form of news that could exist only recently.

Computers are only now getting fast enough and smart enough and interconnected enough so that we can use them as effective tools for gaining wisdom, not just collecting data.  It will take a while for such tools to become ubiquitous, and even longer for a generation to grow up who feels entitled to answers to their questions, and who are adept at using these tools that feel tangible, yet act like programming languages.  I love watching it happen.

Last week I taught a 4th/5th grade class an introduction to computer programming.  It was chaotic, as such classes sometimes are, but fun.  I hear the kids enjoyed it and am planning classs #2 for Friday.  I decided to use Shoes which uses the Ruby language and includes some nice graphics and UI primitives (some notes on why).

Here’s a few notes about the experience:

  • I told the kids that they would be learning Ruby, which I use in my work, and they seemed pretty excited that they were learning a “real” programming language
  • Kids loved the name “Shoes” — laughter and joyous confusion when I was explaining what it was
  • I started the class with a computer history presentation which could have been a full hour long class itself, the kids really liked it and wanted to ask more questions & make
    observations (I ended up spending 25 minutes on this, instead of my planned 10 min). They were really interested in the computer history talk and were excited to hear about the stuff created in their lifetime that they actually knew about and used.  I made sure to mention both women and men in the history (Charles Babbage and Ada Lovelace working on the difference engine; later the two guys who created the hardware and six women who created the software for the ENIAC).  I also made sure to discuss recent history.  Half the class was not yet born in 1998, and for them the Web, Google and YouTube are contemporary with microwave ovens (e.g most people use them). My timeline ended with:

    • 1993: World Wide Web created
    • 1995: Shockwave & Flash first released
    • 1998: Google website launched
    • 2004: FlickR website launched
    • 2005: YouTube website launched
    • 2007: Shoes released!
  • I need to work more on the setup, since I can’t seem to rely on the school computers no matter how much time I spend setting them up (4 hours
    the day before).  There’s all sorts of odd issues with the student accounts not having correct permissions and some computers having special
    cleanup software which wipes everything you just installed when the computer restarts (!)  I was considering putting Shoes and Notepad++ on a USB drive and see if they can just access the installed software from there, but a RailsBridge volunteer is experimenting with creating an Ubunto LiveCD with Shoes which I’m pretty excited about.  Meanwhile, for Friday, I’ve made some progress with install issues on the school computers — cross your fingers for me.
  • What I normally do when I have taught programming before is a coding demo with students following along and typing with me.  I wish I had done that.  Instead, because of the set up glitches, I let them follow the instructions while Chad, another RailsBridge volunteer, and I tried to debug various install issues… and the instructions weren’t really made for that.  Interactive Q&A is a teaching style that works well at this age. I think it would be good to accidentally (on purpose) create a syntax error and have them see me debug it.
  • I didn’t complete my whole lesson plan in the first class… so I have plenty of material for class #2.

I’m publishing the lesson plans as part of the RailsBridge project “teaching kids” which is just starting up — if anyone is interested in collaborating, let me know or just sign up for the RailsBridge google group and chime in.  The worksheets are published on github.

It has been an awesome experience working within the RailsBridge community and the super friendly shoes list.  This is stuff I would be doing anyhow, but with a community of collaborators, I feel like the work I am putting in will not only teach and inspire 28 kids in SF, but perhaps spread to other schools and families through the resources that are being published and the excitement generated on the lists.