Expecting an alias of `new` breaks on Rubinius
Created by: haines
In Draper, we have aliased Decorator.new
to Decorator.decorate
. In one of our specs, I set an expectation on this method and was surprised to find that the expectation wasn't torn down correctly - after the spec, the class no longer responds to decorate
.
I played around with it and it turns out this is only the case for aliases of new
, not other class methods, and it only happens on Rubinius.
Here's a minimal example:
# foo.rb
class Foo
def self.bar; "bar"; end
class << self
alias_method :another_new, :new
alias_method :another_bar, :bar
end
end
# spec/foo_spec.rb
require_relative '../foo'
describe Foo do
describe "class method" do
it "expects", expects: true do
Foo.should_receive :bar
Foo.bar
end
it "calls" do
Foo.bar.should == "bar"
end
end
describe "aliased class method" do
it "expects", expects: true do
Foo.should_receive :another_bar
Foo.another_bar
end
it "calls" do
Foo.another_bar.should == "bar"
end
end
describe "new" do
it "expects", expects: true do
Foo.should_receive :new
Foo.new
end
it "calls" do
Foo.new.should be_an_instance_of Foo
end
end
describe "aliased new" do
it "expects", expects: true do
Foo.should_receive :another_new
Foo.another_new
end
it "calls" do
Foo.another_new.should be_an_instance_of Foo
end
end
end
On Ruby 1.9.3, all specs pass. On Rubinius 2.0.0-dev (1.9 mode):
$ ruby -v
rubinius 2.0.0rc1 (1.9.3 release 2012-11-02 JI) [i686-pc-linux-gnu]
$ rspec --tag ~expects
Run options: exclude {:expects=>true}
....
Finished in 0.00665 seconds
4 examples, 0 failures
$ rspec
.......F
Failures:
1) Foo aliased new calls
Failure/Error: Foo.another_new.should be_an_instance_of Foo
NoMethodError:
undefined method `another_new' on Foo (Class)
# kernel/delta/kernel.rb:81:in `another_new (method_missing)'
# ./spec/foo_spec.rb:44:in `__script__'
# kernel/common/eval19.rb:45:in `instance_eval'
# kernel/bootstrap/array19.rb:18:in `map'
# kernel/bootstrap/array19.rb:18:in `map'
# kernel/bootstrap/array19.rb:18:in `map'
# kernel/loader.rb:696:in `run_at_exits'
# kernel/loader.rb:716:in `epilogue'
# kernel/loader.rb:849:in `main'
Finished in 0.01564 seconds
8 examples, 1 failure
Failed examples:
rspec ./spec/foo_spec.rb:43 # Foo aliased new calls