How do I run a Ruby gem's specs? - ruby

I have forked a ruby gem and made some updates. I need to run the gem tests and add my new tests and ensure all tests are succeeding.
The forked ruby gem is using rspec tests. How can I run these test?

Usually rake is sufficient to run all the tests, regardless of whether they're RSpec, Cucumber, etc. If you want to invoke RSpec directly try running rspec spec instead, or if the gem is using an unconventionally named test directory, just use rspec <directory_name>.
Note: Most new gems these days use Bundler to manage dependencies, so if you don't have the appropriate dependencies and there's a Gemfile in the root, then run "bundle install" first to get them. Then run with bundle exec ... (e.g., bundle exec rspec spec).

Related

Old version of minitest used when running unit tests through rake

I have some classes with unit tests and I have a rake task to run these unit tests. However, I'm running into a problem where when the tests are run via rake an old version of minitest is being used. How can I get rake to use the newer version?
If I use the Minitest::Test subclass it runs fine when the tests are directly run through the ruby
command-line. However, if I use the following rake task:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.pattern = 'tests/**/*_test.rb'
end
When I check the minitest version using puts MiniTest::Unit::VERSION it prints 5.5.0 when run with ruby, but prints 4.3.2 when run with rake. (When using gem list minitest -d version 4.3.2 is listed as the default.)
The reason I want to use the newer version of minitest is that when I directly run the unit tests using Ruby 2.0 I get the following warning:
MiniTest::Unit::TestCase is now Minitest::Test.
However, if I change MiniTest::Unit::TestCase to Minitest::Test I get the following error when I run the tests using rake.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/ext/module.rb:36:in `const_missing': uninitialized constant MiniTest::Test (NameError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/minitest/unit.rb:28:in `const_missing'
I want to avoid requiring any changes to the system configuration, because I want these tests to run on the default OS X ruby installation.
Using gem 'minitest', '=5.5.0' does not change the version of minitest that gets used.
Possibility #1: Run Rake with bundle exec to force it to use the version of Minitest managed by Bundler:
bundle exec rake test
Possibility #2: Create a binstub for Rake for your project that will load your bundle automatically:
bundle binstub rake
After that, you should be able to run Rake without bundle exec to get the same result.
Caveat: This has worked for the Ruby environment managers I've used recently, but you might need to Google around for a solution if it doesn't work for you.
Ruby is having a hard time loading minitest from the gem instead of the standard library, so we need to give it a little help. Add the following to your helper or rake task:
gem "minitest"
That will tell ruby to use the gem version. Just be sure to add that before minitest is required.

Ruby RSpec test for success or failure based on gem version being used

Is there a way to write an Rspec test to prove that some code fails if we are using a specific version of a gem and passes if we use another version of the same gem?
What I do currently is have one version in my Gemfile and then run rspec spec and see a test pass. Then I modify my Gemfile with the different gem version and bundle update and then run rspec spec again and see the same test fail.
I would like to have two tests, one that loads one version of the gem and tests for normal execution and succeeds and another test that loads a different version of the gem and tests for an exception and succeeds on the raised exception and both tests are run on the same rspec spec run. Is this even possible. If not, any suggestions on anything that does not involve me having to modify my Gemfile manually?
Also, in case it helps
I currently use bundler to install gems.
ruby 1.9.3p545
rspec 2.14.1
Also, I am not using Rails
I think you can specify the gem and version you want to use in the code:
it "does something" do
gem "gemname", "4.0"
require "gemname"
expect(subject).to do_something
end
But I don't know if what you're trying to do is a good idea because you should be testing with the same gems you would be using in production.
You could define a different environment to include the alternative version of your gem and use this while testing. For example, in your Gemfile
group :custom_test do
gem 'gemname', 'x.y.z'
end
And then run your tests as in
RAILS_ENV=custom_test rspec
I strongly suggest however that you look into solutions that let you test against different environments, gemfiles, rails and ruby versions (e.g. Jenkins or Travis etc.)

Why doesn't autotest run my rspec tests (ruby no rails)?

I've used autotest in other project on my machine.
I'm now doing a small ruby project that uses rspec.
I can run the tests with rspec spec and they all run.
I've added 'autotest' to my Gemfile and I've bundle'd.
I can run autotest but it doesn't run my rspec tests in spec/
I've added empty .rspec and .autotest files.
I've tried both the autotest and the autotest-standalone gems.
I've reviewed all the info at http://ph7spot.com/musings/getting-started-with-autotest#troubleshooting_autotest_test_detection which was helpful but didn't solve my question.
Turns out I needed (and didn't have)
gem 'rspec'
in my Gemfile (and then bundle of course)
I hadn't realized this because with rspec on my machine from other projects and thus the ability to do rspec spec 'manually', I didn't realize that I needed to list the gem explicitly i.e. in the Gemfile.
I also verified that:
I DO need the .rspec file It's OK if it's empty but it needs to exist.
It can be created with touch .rspec on *nix.
I do NOT require a .autotest file.

setting up Rspec

I'm trying to run rspec on my machine for the first time. I did gem install rspec in the global gemset. However, when I try to do
spec intranet_reader_spec.rb
I get
-bash: spec: command not found
And when I try to do
bundle exec rspec spec intranet_reader_spec.rb
I get
Could not locate Gemfile
(Note, I don't have a gemfile. I'm just running one test script from a ruby book)
I found these instructions for adding spec to the path, i.e. put the following in bashrc
export PATH=$PATH:/Users/<UserName>/.gem/ruby/1.8/bin
However, that didn't work. I note that on my mac. if I navigate to .gem/ruby/1.8 and do 'ls', it doesn't have a 'bin.' In the 1.8 folder, it has
cache doc gems specifications
Can anyone tell me what I might be doing wrong?
If you don't have a Gemfile then you can't run bundle exec.
Also, the command for RSpec 2.x is rspec. You might be following an outdated tutorial, since the command was spec in version 1.x.
Just run:
rspec intranet_reader_spec.rb

An easy way to run tests on a gem?

Is there a quick and easy way to test a gem which is already installed locally? Like:
gem test gem_name_to_test
rubygems docs says one can put gem: --run-tests in ~/.gemrc file to run unit tests when a gem is installed. I could not make it work though and that is not exactly what I need.
You can navigate to the place the gem lives and run tests from there, so for example:
$ cd ~/.rvm/gems/ruby-1.9.2-p290/gems/awesome_print-0.4.0
$ rake spec
Note that additional dependencies may need to be installed via bundler or gem
There is "gem test" command, which may or may not be what you are looking for. It run tests agains the package and sends them to test.rubygems.org.
gem install rubygems-test
gem test gem_name_to_test

Resources