Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing code in a Rails engine with RSpec

I'm trying to do something seemingly simple but it's proven rather hard.

I want to write tests using RSpec for classes that I've put in the lib directory of a Rails Engine.

Here are exactly the steps I'm using:

rails plugin new mygem -T --mountable --full --dummy-path=spec/dummy

Then I cd mygem; vim mygem.gemspec

I add the following line to mygem.gemspec:

s.add_development_dependency "rspec-rails"

I run bundle install; rails generate rspec:install

Then I edit ~/mygem/lib/mygem/engine.rb adding the following:

module Mygem
  class Engine < ::Rails::Engine
    isolate_namespace Mygem

    config.generators do |g|
      g.test_framework :rspec
    end
  end
end

I create a very simple class in the lib directory, ~/mygem/lib/mygem/something.rb

and add the following:

module Mygem
  class Something
    def hi
      "hi"
    end
  end
end

Create a test file ~/mygem/spec/something_spec.rb

then add the following:

require 'rails_helper'

describe Mygem::Something do
  it 'says hi' do
    s = Mygem::Something.new
    expect(s.hi).to eq('hi')
  end
end

And boom, I get the following output:

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 `<top (required)>'
  from ~/Documents/mygem/spec/something_spec.rb:1:in `require'
  from ~/Documents/mygem/spec/something_spec.rb:1:in `<top (required)>'
  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 `<top (required)>'
  from ~/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `load'
  from ~/.rvm/gems/ruby-2.2.2/bin/rspec:23:in `<main>'
  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 `<main>'

Any tips as to what I may be doing incorrectly?

Thanks in advance!

like image 873
DaniG2k Avatar asked Feb 10 '23 06:02

DaniG2k


2 Answers

It looks like your rails_helper is looking for an environment.rb file to load, which doesn't exist in a Rails engine. It does, however, exist in your dummy app, which is what RSpec is run against.

Try adding this into the top of your rails_helper file:

require File.expand_path("../dummy/config/environment.rb", __FILE__)
like image 193
Catherine Cai Avatar answered Feb 16 '23 20:02

Catherine Cai


Finally got it working after the following steps (thanks ccai for the suggestion):

in rails_helper.rb:

# Comment this line:
# require File.expand_path('../../config/environment', __FILE__)
require File.expand_path("../dummy/config/environment.rb", __FILE__)

then in lib/mygem.rb

require "mygem/something"

Works!

like image 29
DaniG2k Avatar answered Feb 16 '23 21:02

DaniG2k