JRuby 1.6.2
rspec-core (2.6.4)
rspec-expectations (2.6.0)
rspec-mocks (2.6.0)
rspec-rails (2.6.1)

I'm sure this has more to do with the way JRuby wraps Java exceptions but I 
figured I'd post here in case anyone here has any insight or pointers. In the 
context of writing a spec for a model like thing that wraps legacy Java code, I 
found myself attempting to stub a method on a Java Exception rescued in the 
implementation.

eg.

describe '#valid?' do
....
it 'adds validation exceptions raised by service to #errors' do
      ve = ValidationException #a java exception
      ve.stub(:localized_message).and_return('a bunch of errors')

      service.stub(:validateTaskForSave).and_raise(ve)

      subject.valid?

      subject.errors.should == ['a bunch of errors']
end

the above example fails because the :localize_message stub is ignored and 
instead the real implementation receives the message. I know this typically 
screams typo but not in this case. Here is a more boiled down version:

specify 'rescued exception message should be "bar" because I stubbed it' do
      begin
        e = Java::java.lang.NullPointerException.new('foo')
        e.stub(:message).and_return('bar')
        raise e
      rescue Java::java.lang.NullPointerException => e
        e.message.should == 'bar'
      end
end

Failure/Error: e.message.should == 'bar'
       expected: "bar"
            got: "foo" (using ==)

That seemed odd to me but maybe moot anyway since in reality I would be need to 
rescue a NativeException masquerading as my target exception.

e.g. This code

def valid?
    begin
      service.validateTaskForSave(task)
    rescue ValidationException => e
      puts "rescued exception: #{e.class.name}"
      .....

prints "rescued exception: NativeException"  when hit through the console.

So how would one simulate a NativeException if needed (i.e. you want to stub 
methods on it)?

-lenny





_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to