Created
June 10, 2013 12:16
-
-
Save latentflip/5748310 to your computer and use it in GitHub Desktop.
Testing Puts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass | |
def initialize(word) | |
@word = word | |
end | |
def putter | |
puts @word | |
end | |
end | |
describe "MyClass" do | |
it "should put 'foo'" do | |
myobj = MyClass.new('foo') | |
myobj.should_receive(:puts).with('foo') | |
myobj.putter | |
end | |
end |
Notably, puts
is not mixed into ruby 1.9's BasicObject, so if you try to do this:
class MyClass < BasicObject
def putter
puts 'foo'
end
end
MyClass.new.putter
You'll get:
NoMethodError: undefined method `puts' for #<MyClass:0x00000100cdfae0>
from (irb):3:in `putter'
from (irb):6
from /Users/Roberts/.rvm/rubies/ruby-1.9.3-p125-perf/bin/irb:16:in `<main>'
Which is kinda confusing, till you remember puts is just a method on your object, and BasicObject is a very bare object so has no puts.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
puts
is just a method that is mixed in to almost every object, so when you call puts, you are really callingself.puts
which means you can use RSpecs mock expectations.