Happynoff
Happynoff

Reputation: 1375

Testing rails engine helpers with rspec

I've searched a bit but I can't find a good explanation about it so here is my question.

I'm writing a Rails engine that only adds helpers.

The structure is like this (the important part):

my_engine/
  -- app/
     -- helpers/
        -- my_engine/
           -- my_engine_helper.rb
  -- spec/
     -- dummy/
     -- spec_helper.rb

My spec_helper.rb is the following:

# Configure Rails Envinronment
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../dummy/config/environment.rb',  __FILE__)

require 'rspec/rails'

ENGINE_RAILS_ROOT = File.join(File.dirname(__FILE__), '../')

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(ENGINE_RAILS_ROOT, 'spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end

So far so good, but I'm not sure about how to test my helper. I would like to know what is the best way to:

For now I get the following error when running the spec:

undefined local variable or method `helper' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fcee8b0f4a0>

with the following test:

require 'spec_helper'

module MyEngine
  describe MyEngineHelper do
    describe '#something' do
      it "does something" do
        helper.something.should be_true
      end
    end
  end
end

Thank you !

Upvotes: 3

Views: 1868

Answers (2)

jmgarnier
jmgarnier

Reputation: 2668

I would check out how activeadmin does this

https://github.com/gregbell/active_admin/blob/master/spec/unit/helpers/settings_spec.rb

This is a rails engine with a very high quality test suite (I contributed to it)

Upvotes: 1

Ryan Bigg
Ryan Bigg

Reputation: 107718

Move it to spec/helpers/my_engine_helper_spec.rb. The rspec-rails has a hook for things in spec/helpers to add the helper method.

Upvotes: 2

Related Questions