Stub unimplemented method in rspec

subject(:klass) do 
  Struct.new(:name) do
   include MyModule
  end
end

http://ruby-doc.org/core-2.2.0/Struct.html


RSpec raises this exception because it is not useful to stub a method that does not exist on the original object.

Mocking methods is always error-prone because the mock might behave differently than the original implementation and therefore specs might be successful even if the original implementation would have returned an error (or does not even exist). Allowing to mock non-existing methods is just plain wrong.

Therefore I would argue that you should not try to bypass this exception. Just add a name method to your class that raises a clear exception if run outside of the test environment:

def self.name
  raise NoMethodError  # TODO: check specs...
end