尝试在 Mocha 中使用参数测试方法时出现奇怪的错误。是bug还是我的问题?

发布于 2024-09-03 06:31:48 字数 1970 浏览 4 评论 0原文

很难找到任何关于 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)
  end
end

是的,这是人为的,但它是您可能不想在测试中调用的方法的想法的简化。

所以我可能会编写一个测试,例如:

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

现在我希望它能够通过。但相反,我收到了一条相当神秘的错误消息:

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'

这对我来说绝对没有任何意义,除了摩卡肠子深处的东西刚刚发出叮当声。如果我编写相同类型的测试而不将参数传递给第二种方法,那么我就没有问题。我错过了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-09-10 06:31:48

我认为这一定是应该造成问题的原因。我使用测试/单元,一切似乎都正常。

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

给出以下输出:

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 #  

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 #  
没有伤那来痛 2024-09-10 06:31:48

抱歉 - 我才刚刚看到这个。最好在 Lighthouse 中向我们提交错误报告。你找到了什么文档?您看过 Rubyforge 上的 RDoc 吗?您正在寻找哪些类型的文档但没有找到?

我无法重现你的错误。 Ruby、Rubygems、Shoulda 和 Shoulda 的版本是什么?你用的是摩卡吗?

您可以在此要点中查看我运行测试的结果。

Sorry - I've only just seen this. It's better to submit bug reports to us in Lighthouse. What documentation have you found? Have you seen the RDoc on Rubyforge? What sort of documentation were you looking for that you did not find?

I've been unable to reproduce your bug. What version of Ruby, Rubygems, Shoulda & Mocha were you using?

You can see the results of me running your test in this Gist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文