the scan artist

Paul Morganthall

Locating Sinatra views

I have adhered to strict TDD as I build cheap buster. There wasn’t much need for views until now as I have been working on the internal API. The original index action contained the simplest Sinatra ‘view’, a string.

1
2
3
get '/' do
  '<h1>cheap buster</h1>Welcome'
end

Now that I’m ready to build the first form, I figured the first step was to move the view to a separate file.

1
2
3
get '/' do
  erb :index
end

This works fine in the browser, but the test looks for the view one level down in test/views:
No such file or directory - /Users/adam/dev/cheap_buster/test/views/index.erb

The only mention I could find of this behavior was an article by Tobias Crawley from 2008. He fixed the problem by linking test/views to views, but that feels extreme. I looked through lots of Sinatra applications in the wild. Many had no tests, some didn’t test views. None did anything special to help the tests locate the views.

This feels like some kind of configuration error on my part. For now, I’ve solved the problem by adding a configuration setting to the affected tests:
app.set :views, File.expand_path('./views')

Hacky as that seems, at least the fix is quite localized, sitting right next to the tests that need the workaround.