Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created December 2, 2010 13:03
Show Gist options
  • Save floehopper/725248 to your computer and use it in GitHub Desktop.
Save floehopper/725248 to your computer and use it in GitHub Desktop.
require "test/unit"
require "rubygems"
require "shoulda"
require "mocha"
class Red
def gets(*args)
@input.gets(*args)
end
def puts(*args)
@output.puts(*args)
end
def initialize
@input = $stdin
@output = $stdout
end
private
def first_method
input = gets.chomp
if input == "test"
second_method(input)
end
end
def second_method(value)
puts value
# second_method(value) # this would result in infinite recursion
end
end
class FooTest < Test::Unit::TestCase
context "foo" do
setup do
@project = Red.new
@project.instance_variable_set(:@input, StringIO.new("test\n"))
@project.stubs(:second_method)
end
should "pass input value to second_method" do
@project.expects(:second_method).with("test").once
@project.instance_eval {first_method}
end
end
end
@floehopper
Copy link
Author

$ ruby Desktop/weird_error.rb 
Loaded suite Desktop/weird_error
Started
.
Finished in 0.000811 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

@floehopper
Copy link
Author

If I comment out line #21 i.e. the call to Red#second_method from within Red#first_method, I get the test failure I would expect :-

$ ruby Desktop/weird_error.rb
Loaded suite Desktop/weird_error
Started
F
Finished in 0.0094 seconds.

  1) Failure:
test: foo should pass input value to second_method. (FooTest)
    [Desktop/weird_error.rb:38:in `__bind_1291295490_929583'
     /Users/jamesmead/.rvm/gems/ree-1.8.7-2010.02/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `call'
     /Users/jamesmead/.rvm/gems/ree-1.8.7-2010.02/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `test: foo should pass input value to second_method. ']:
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: #<Red:0x101705ab0>.second_method('test')
satisfied expectations:
- allowed any number of times, not yet invoked: #<Red:0x101705ab0>.second_method(any_parameters)

1 tests, 1 assertions, 1 failures, 0 errors

@floehopper
Copy link
Author

$ ruby -v
ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.4.0], MBARI 0x6770, Ruby Enterprise Edition 2010.02
$ gem -v
1.3.7
$ gem list mocha

*** LOCAL GEMS ***

mocha (0.9.10)
$ gem list shoulda

*** LOCAL GEMS ***

shoulda (2.11.3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment