views:

91

answers:

1

Hi there.

It's rather hard to find any documentation on Mocha, so I'm afraid I'm totally at sea here. I have found a problem with stubbing methods that pass arguments. So for instance if I set up a class like this:

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)
  end
end

Yes it's contrived, but it's a simplification of the idea that you may have a method that you don't want called in the test.

So I might write a test such as:

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

Now I would expect this to pass. But instead I get this rather arcane error message:

Errno::ENOENT: No such file or directory - getcwd
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/backtrace_filter.rb:12:in `expand_path'
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/backtrace_filter.rb:12:in `block in filtered'
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/backtrace_filter.rb:12:in `reject'
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/backtrace_filter.rb:12:in `filtered'
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/expectation_error.rb:10:in `initialize'
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/mockery.rb:53:in `new'
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/mockery.rb:53:in `verify'
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/api.rb:156:in `mocha_verify'
/Users/i0n/.rvm/gems/ruby-1.9.2-head/gems/mocha-0.9.8/lib/mocha/integration/mini_test/version_131_and_above.rb:27:in `run'

This means absolutely nothing to me, other than something deep in Mochas bowels has just gone clang. If I write the same sort of test without an argument passing to the second method I get no problem. Am I missing something?

A: 

I think it must be something in shoulda causing the problem. I use test/unit, and everything appears to be OK.

require 'rubygems'
require "test/unit"
require 'mocha'
require File.dirname(__FILE__) + '/../src/red'

class RedTest < Test::Unit::TestCase

    def setup
        @project = Red.new   
        @project.instance_variable_set(:@input, StringIO.new("test\n"))              
        @project.stubs(:second_method)
    end


    def test_description_of_thing_being_tested
        @project.expects(:second_method).with("test").once
        @project.instance_eval {first_method} 
    end

end

gives the following output:

stephen@iolanta:~/tmp/red/test #  ruby red_test.rb 
Loaded suite red_test
Started
.
Finished in 0.000679 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
stephen@iolanta:~/tmp/red/test #  
stephenr