Saturday, March 16, 2013

Kul Testing

I've been putting off unit testing Kul as I really wasn't sure what sort of testing was possible against a web framework. Turns out that Rack apps (such as Sinatra) have this really awesome testing framework called 'rack-test'. It works really well, and allows you to do nifty things like this:
  it 'serves the favicon' do
    get '/favicon.ico'
    last_response.should be_ok
  end
Cool, right? You can also look at last_response.body for the actual body of the HTML returned, examine the headers, etc.

This was the simple part. The difficult part was setting up the external files for my tests.

What I wanted was a set of folders underneath the spec folder that contain my "test application". I would run the RSpec tests against that known application and test the results. It's the same way that Sinatra handles testing, and it makes perfect sense to me.

What I couldn't figure out was how to set the current working directory to the test application folder. I spent an hour searching Google trying to find the answer. I tried setting the Sinatra root folder, tried to figure out where Tilt was getting its templates from and hijack that, tried configuring RSpec to use a different folder - none of it seemed to work. The tests worked fine when I ran them from the test application folder, but that seemed like a really fragile way to do things.

I looked all over for an example to go off of, but I didn't really see anyone else extending Sinatra into a gem. I tried throwing every combination of the terms "sinatra rspec test gem views set folder directory" and nothing told me how to fix the problem. There were a couple posts that talked about setting some crazy internal Sinatra method, but that seemed like a really complex solution to a simple problem.

I eventually stumbled across a post that talked about the Ruby current working directory. "There's no way it could be that simple." I thought.

So I dug into the source. I was calling the Sinatra 'send_file' method, so I found that and looked at it. And that's when I realized that Sinatra wasn't doing ANYTHING to the path.

So, yes. It was that simple. In my spec_helper.rb file, I added this line:
Dir.chdir File.join(File.dirname(__FILE__), 'test_files')
And then my tests started passing no matter what folder I was in when I typed 'rspec spec'. And there was happiness in the land.

Now all I have to do is actually WRITE the tests.  :)

No comments:

Post a Comment