TBH as a gem maintainer I have never needed to run gem tests against some specific release. But it seems the following approach should work in your case:
- use
require "<my-gem>" in tests
- run tests with
bundler - bundle exec rake ...
- have 2
Gemfile files to run tests with local source code and with installed locally gem
Developing a gem you usually include dependencies declared in a gemspec file into a Gemfile file with the gemspec declaration:
# Gemfile
gemspec
# development dependencies
# ...
It adds into a Gemfile.lock file declaration to use source code of the developed gem (my-gem) from source code located in the current directory. For instance for a gem I am maintaining (dynamoid) it looks like the following:
PATH
remote: .
specs:
dynamoid (3.11.0)
activemodel (>= 4)
aws-sdk-dynamodb (~> 1.0)
concurrent-ruby (>= 1.0)
More details you may find here - https://bundler.io/guides/rubygems.html.
In the second Gemfile to run tests agains a released version of your gem you may require explicitly my-gem with a specific version instead of using gemspec method:
# Gemfile
gem "<my-gem>", "1.0"
# development dependencies
# ...
As an option you can use a single Gemfile and check some env variable to either use gemspec method or declare explicitly a dependency on my-gem:
# Gemfile
if ENV['MY_GEM_VERSION']
gem 'my-gem', ENV['MY_GEM_VERSION']
else
gemspec
end
# development dependencies
Usually I use the following dirty hack to run some gem's tests locally:
- install dependencies with
bundle install command
- use
ruby -I ./lib -I ./test rake test to run tests agains the source code
- use
rake test to run tests agains an installed gem version
require 'my_gem'in your tests. Adding yourlibdirectory to$LOAD_PATHshould do the trick. In Rake there'sRake::TestTask#libs.lib/gem_name.rbfile will handle requiring all the parts so your tests should be requiring this file instead of loading files individually. Some gems are split into sections which let you just require parts of the gem - ActiveSupport is a really good example of this.