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

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.

Related

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.)

Easier way to test gem

I am in the middle of writing a Ruby Gem, but was wondering if there is an easier way to "test" your gem without having to build it, install it and require it to test it out in an IRB console?
Git clone it in an arbitrary folder, add its lib path to $: in the rakefile if needed (you actually don't in this case, as RakeTest should add it for you), and run rake test directly.
From irb, proceed similarly: add the lib path to $: to bypass the packaged gem. But note that you'll need to reload it when you change it, so it's less convenient than rake.
For completeness, in case you or a future visitor is unfamiliar with Rake:
Running Ruby unit tests with Rake

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

How do I run a Ruby gem's specs?

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).

Gem Development Workflow

I'm working on a fork of someone's gem that is a command line utility. Here's a general overview of the directory structure:
bin/
bin/foo
lib/
lib/foo.rb
lib/foo/bar.rb (etc)
To test it, I normally do something like this:
cd bin/
./foo <args>
But I want to be able to use it from any directory (like it would be once installed). My question is if it's possible to achieve this without installing the gem on my system each time.
My first attempt at this was to create a symbolic link to the foo script that was on my PATH, but this messes with the require 'foo' line in the script since File.dirname(__FILE__) now refers to wherever the symbolic link was created.
Is there a common way of doing this?
(Oh, and here's the relevant lines from the foo script)
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'rubygems'
require 'foo'
(EDIT)
I'm aware of the normal ways of testing a library (ie rake test, etc)--I'm specifically interested in using the script from any directory without reinstalling the gem with every change (if possible).
In almost every gem I've looked into, there is a rakefile of some sort. In which case you go to the root of the gem, and go:
rake test
For a list of tasks, use:
rake -T
(This assumes you have rake installed in the first place, obviously: gem install rake if not.)
Also, many a gem also features a gemfile. In this case you can use bundler to install applicable dependencies (in particular, test suites and development-related gems).

Resources