title: Testing code in a Rails engine with RSpec
body: <p>I'm trying to do something seemingly simple but it's proven rather hard.</p>
  
  <p>I want to write tests using RSpec for classes that I've put in the <code>lib</code> directory of a Rails Engine.</p>
  
  <p>Here are exactly the steps I'm using:</p>
  
  <pre><code>rails plugin new mygem -T --mountable --full --dummy-path=spec/dummy
  </code></pre>
  
  <p>Then I <code>cd mygem; vim mygem.gemspec</code></p>
  
  <p>I add the following line to mygem.gemspec:</p>
  
  <pre><code>s.add_development_dependency "rspec-rails"
  </code></pre>
  
  <p>I run <code>bundle install; rails generate rspec:install</code></p>
  
  <p>Then I edit <code>~/mygem/lib/mygem/engine.rb</code> adding the following:</p>
  
  <pre><code>module Mygem
    class Engine &lt; ::Rails::Engine
      isolate_namespace Mygem
  
      config.generators do |g|
        g.test_framework :rspec
      end
    end
  end
  </code></pre>
  
  <p>I create a very simple class in the lib directory, <code>~/mygem/lib/mygem/something.rb</code></p>
  
  <p>and add the following:</p>
  
  <pre><code>module Mygem
    class Something
      def hi
        "hi"
      end
    end
  end
  </code></pre>
  
  <p>Create a test file <code>~/mygem/spec/something_spec.rb</code></p>
  
  <p>then add the following:</p>
  
  <pre><code>require 'rails_helper'
  
  describe Mygem::Something do
    it 'says hi' do
      s = Mygem::Something.new
      expect(s.hi).to eq('hi')
    end
  end
  </code></pre>
  
  <p>And boom, I get the following output:</p>
  
  <pre><code>rspec
  ~/Documents/mygem/spec/rails_helper.rb:3:in `require': cannot load such file -- ~/Documents/mygem/config/environment (LoadError)
    from ~/Documents/mygem/spec/rails_helper.rb:3:in `&lt;top (required)&gt;'
    from ~/Documents/mygem/spec/something_spec.rb:1:in `require'
    from ~/Documents/mygem/spec/something_spec.rb:1:in `&lt;top (required)&gt;'
    from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'  from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
    from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
    from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
    from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
    from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
    from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
    from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
    from ~/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.2/exe/rspec:4:in `&lt;top (required)&gt;'
    from ~/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `load'
    from ~/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `&lt;main&gt;'
    from ~/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
    from ~/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `&lt;main&gt;'
  </code></pre>
  
  <p>Any tips as to what I may be doing incorrectly?</p>
  
  <p>Thanks in advance!</p>