I have been using SimpleCov to measure the code coverage of my specifications, and it has been excellent. I would recommend it, if only for how easy it was to get it working with RSpec.

I have run into one problem though – running it under Spork generates no results! As far as I can tell, this is a problem with the way Spork caches your application files. SimpleCov uses the Ruby 1.9 built in profiling features, and either 1.9 or Spork need the files under inspection to be loaded after the profiling has started. Unfortunately your application files aren’t reloaded when you are using Spork, that’s kind of the point!

Some people seem to be able to get away with merely turning off application file caching in config/environments/test.rb, but that didn’t work for me. Instead, I have come up with a different solution: Only turn on code coverage reporting when I run RSpec outside of Spork. At the top of spec/spec_helper.rb I have the following block:

1
2
3
4
5
6
7
if(ENV["RUN_COVERAGE"])
  require 'simplecov'
  SimpleCov.start 'rails' do
    add_filter "/spec/"
  end
  puts "Running coverage tool\n"
end

That way I can run RUN_COVERAGE=true rspec spec/ in the root of my application directory, and RSpec will run with SimpleCov enabled. As an added bonus, the code coverage metrics aren’t running on every test run, so my tests are faster!

 

Edit: I have updated this for Bootstrap 2. See the links at the bottom of the page for both versions.

I have been working on a website in my spare time to help me organise my life a bit better, and one of the things that always trips me up with these kinds of projects is the visual style. For a while I had a basic fixed width style I used, but it didn’t really fit properly with what I wanted to build. A little while after I had started, Twitter released Bootstrap, a nice looking CSS framework. I decided to have a go at using it on my site (in a separate git branch of course) and it ended up only taking me less than an hour to get it all up and looks alright.

One thing that bothered me though was the repetition in my forms – the format for showing labels that line up nicely next to the form elements was the same across all input types, and so was the error handling. This got me thinking: why should I retype all of this when the Rails FormBuilder class is already doing half the work? Surely I could just override some of the methods to make this work?

I needed to learn more about Ruby metaprogramming to get it to work – I still wanted the core functionality of the default rails FormBuilder, just with some extra DOM nodes wrapped around it. Ruby lets you redefine methods at runtime, but also allows you to alias the old methods so you can refer to them inside your new version of the method. So I ended up writing a method that does the Bootstrap boilerplate DOM wrapper creation and calls the original method to generate the input element itself.

I still haven’t overridden all of the FormBuilder methods because I need to actually test them (and I don’t have a use for them yet), but the basics are here in this Gist: https://gist.github.com/1236567. The methods I have overridden are in basic_helpers, the ones that output multiple inputs are in multipart_helpers. The great thing is I have only had to add method names to those two arrays as needed – none of the input types have needed special casing. This might not be the case for all of them, but one can only hope. If you do find this useful, or want to add and test some more of the helpers, please form my Gist and leave me a comment here.

Edit: Here are links to both the v1 and v2 form builders:

Version 1 form builder:
https://gist.github.com/1236567
Version 2 form builder:
https://gist.github.com/1829710

 

 

After having a quick look around the net (and not finding anything up to date and useful) I came up with the following basic test structure for testing my ApplicationHelper methods that served up content_for things like page titles, javascript includes, etc:

1
2
3
4
5
6
7
8
9
10
require 'spec_helper'
 
describe ApplicationHelper do
  describe "title" do
    it "should call content_for(:title) with the title passed" do
      helper.title("title stuff")
      helper.content_for(:title).should == "title stuff"
    end
  end
end
© 2011 Jamie's Space Suffusion theme by Sayontan Sinha