Ruby 在Mocha中尝试使用参数测试方法时出现奇怪的错误。是虫子还是我?
很难找到关于摩卡咖啡的任何文件,所以我恐怕在这里完全不知所措。我发现传递参数的存根方法有问题。例如,如果我设置了这样一个类:Ruby 在Mocha中尝试使用参数测试方法时出现奇怪的错误。是虫子还是我?,ruby,unit-testing,shoulda,ruby-mocha,Ruby,Unit Testing,Shoulda,Ruby 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 d
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'
这对我来说毫无意义,除了莫查斯肠深处的东西发出叮当声。如果我编写相同类型的测试,而不将参数传递给第二个方法,那么我就不会有问题。我遗漏了什么吗?我想这一定是应该达中的什么东西造成的问题。我使用test/unit,一切看起来都正常 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 需要“rubygems” 需要“测试/单元” 需要“摩卡” require File.dirname(_File__)+'/../src/red' 类RedTest
对不起,我刚看到这个。最好在一个月内向我们提交bug报告。你找到了什么文件?你看到那张照片了吗?您在寻找什么类型的文档,但没有找到 我无法复制你的错误。您使用的是什么版本的Ruby、Rubygems、Shoulda&Mocha 您可以看到我在中运行测试的结果