dkubb / adamantium

Create immutable objects
MIT License
373 stars 13 forks source link

instances of classes that `include Adamantium` have their singleton classes frozen #23

Closed misfo closed 10 years ago

misfo commented 11 years ago

Here's a minimal case:

p RUBY_VERSION #=> "2.0.0"

require 'adamantium'
require 'adamantium/version' # not required by default, oops!

p Adamantium::VERSION #=> "0.1.0"

class Something
  include Adamantium
end

p Something.new.singleton_class.frozen? #=> true

This prevents Rspec from being able to mock methods on these instances

misfo commented 11 years ago

It turns out this is a more general problem with Object#freeze:

obj = Object.new
obj.singleton_class.frozen?
#=> false

obj.freeze
obj.singleton_class.frozen?
#=> true

There has to be a way to mock methods on frozen objects though...

mbj commented 11 years ago

@misfo I typically do not mock methods on a "random" object anymore. The ROM development style leads to very "small" public interface objects, so I generally pass a full instance in, or in rare cases a full mock. But no need to mock one of many methods if an instance.