Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Thursday, November 18, 2010

RSpec 2: #raise_error

I was writing a spec and attempted to use #raise_error. To my surprise, it wasn't working. Here's what I did and how I solved it.

Code to be tested:
class TestClass
  def run; raise "Error"; end
end

Spec:
it 'should raise' do
  TestClass.new.run.should raise_error
end

To my surprise, the error wasn't caught and it failed with a raised error. There was no syntax error and the way I wrote it felt very natural, but it wasn't behaving the way I thought it should. However, upon further investigation, I realized the usage of #raise_error is on a Proc or lambda.

The spec should have been:
it 'should raise' do
  Proc.new { TestCase.new.run }.should raise_error
  lambda { TestCase.new.run }.should raise_error
end

And if you really want it to read well:
it 'should raise' do
  expect { TestCase.new.run }.to raise_error
end

Thursday, November 11, 2010

Fixing Ruby on Ubuntu

Before using RVM, I was using the default Ruby installation on Ubuntu. Then, I needed different versions of Ruby and started messing around with installing other Rubies. Apparently, I messed up my default Ruby install without knowing it. Then, I moved off to RVM and never looked back.

Recently, I needed my system Ruby again. This is when I found out it was completely messed. It kept giving me this error when I tried to use RubyGems.
'require': no such file to load -- thread (LoadError)

This was supposed to be impossible since thread is in the core library of Ruby. Then, I tried other things like requiring pp and date. Both gave me the same error.

I attempted to reinstall Ruby.
sudo apt-get purge ruby ruby-dev ruby1.8 ruby1.8-dev rubygems rubygems1.8
sudo apt-get install ruby ruby-dev rubygems

Unfortunately, this didn't help at all. I tried this several times to no avail. I then tried installing RubyGems through the source, but I couldn't even run setup.rb since it requires core libraries.

I decided to look through Synaptic Package Manager for anything that matched Ruby. That's when I realized I hadn't uninstalled all of Ruby. I had missed the Ruby libraries! Duh! I had messed up my Ruby libraries and wasn't reinstalling it.
sudo apt-get purge libruby libruby1.8
sudo apt-get install libruby

This fixed my require problems and all was right again.