Created
October 22, 2012 19:49
-
-
Save sriprasanna/3933642 to your computer and use it in GitHub Desktop.
How to test a ruby method that does nothing
This file contains 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
require "rubygems" | |
require "rspec" | |
class TestNothing | |
def do_nothing | |
end | |
def return_nil | |
nil | |
end | |
def do_something | |
1 + 1 | |
end | |
end | |
describe "TestNothing" do | |
def method_should_do_nothing(klassname, methodname) | |
has_called = false | |
set_trace_func proc { |event, file, line, id, binding, classname| | |
if classname.to_s == klassname | |
has_called = true | |
event.should_not eq("line"), "Err! #{methodname} is supposed to do nothing" | |
end | |
} | |
instance = Object.const_get(klassname).new | |
instance.send(methodname) | |
has_called.should be_true | |
end | |
context "Method should do nothing" do | |
it "should pass" do | |
method_should_do_nothing("TestNothing", "do_nothing") | |
end | |
it "should return nil and still pass" do | |
method_should_do_nothing("TestNothing", "return_nil") | |
end | |
it "should raise an exception when it does something" do | |
expect{ method_should_do_nothing("TestNothing", "do_something") }.to raise_error | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment