Monday, March 23, 2015

undefined method `capture_haml' for RSpec

If you're using Haml in a Rails project, testing with RSpec, you might encounter the "undefined method 'capture_haml' for ..." error while trying to run your specs. I encountered this error in a view spec when trying to use Simple Form along with Haml. Fortunately, like many other obscure bugs there is a quick and dirty solution: The init_haml_helpers section is necessary to prevent other errors from happening.

Note that all lines are required. If you drop the init_haml_helpers line, you'll get the following error:
ActionView::Template::Error: undefined method `capture_position=' for nil:NilClass
and of course if you don't have any of this, you will get:
ActionView::Template::Error: undefined method `capture_haml' for #<RSpec::ExampleGroups::...