Thomas Preißler
Thomas Preißler

Reputation: 623

How to test a helper within a gem with rspec?

I already published my first rails gem but not yet with any tests included. This gem is just a simple helper which I want to test with rspec. But I have no idea how I can test a helper within a gem with rspec.

I tried to create a file spec/helper/development_ribbon_helper_spec.rb:

require 'spec_helper'
describe DevelopmentRibbonHelper do
end

But when I execute rspec I get this error:

development_ribbon_helper_spec.rb:3:in `<top (required)>': uninitialized constant DevelopmentRibbonHelper (NameError)

You can find the whole source code on github.

Upvotes: 2

Views: 1013

Answers (1)

rossta
rossta

Reputation: 11504

It looks like you may be trying to write a rails engine gem, correct? Since your helper file is located outside of lib/, your spec_helper will need to know how to load your rails engine.

One way to do this is to create a dummy rails app in your spec/ directory. This setup is well-described in this post: http://reinteractive.net/posts/2-start-your-engines.

Alternatively, you can move the helper file into lib/development_ribbon and require it in your development_ribbon.rb file:

require "development_ribbon/development_ribbon_helper"

Then, in your rails apps that use the gem, you could include the helper in application_helper.rb

module ApplicationHelper
  include DevelopmentRibbon::DevelopmentRibbonHelper
end

Upvotes: 2

Related Questions