Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 摩卡咖啡:我可以放一个;决不;对带参数的存根方法的期望?_Ruby On Rails_Ruby On Rails 3_Unit Testing_Testing_Mocha.js - Fatal编程技术网

Ruby on rails 摩卡咖啡:我可以放一个;决不;对带参数的存根方法的期望?

Ruby on rails 摩卡咖啡:我可以放一个;决不;对带参数的存根方法的期望?,ruby-on-rails,ruby-on-rails-3,unit-testing,testing,mocha.js,Ruby On Rails,Ruby On Rails 3,Unit Testing,Testing,Mocha.js,我的问题与此类似: 在obj上执行操作不会像我预期的那样调用dosomething(:apples)。但是,它可能会调用do\u something(:banana)。如果是这样的话,我会得到一个意外的调用失败 我的理解是,由于我将never放在期望的末尾,它只适用于特定的修改期望。然而,一旦我开始在obj上模仿行为,我就在某种意义上“搞砸了” 如何允许对obj上的do\u something方法进行其他调用 编辑:以下是一个清晰的例子,完美地说明了我的问题: describe 'mocha'

我的问题与此类似:

在obj上执行操作不会像我预期的那样调用dosomething(:apples)。但是,它可能会调用
do\u something(:banana)
。如果是这样的话,我会得到一个意外的调用失败

我的理解是,由于我将
never
放在期望的末尾,它只适用于特定的修改期望。然而,一旦我开始在
obj
上模仿行为,我就在某种意义上“搞砸了”

如何允许对
obj
上的
do\u something
方法进行其他调用

编辑:以下是一个清晰的例子,完美地说明了我的问题:

describe 'mocha' do
  it 'drives me nuts' do
    a = mock()
    a.expects(:method_call).with(:apples)

    a.lol(:apples)
    a.lol(:bananas) # throws an unexpected invocation
  end 
end

这与其说是一个解释摩卡行为的真实答案,不如说是一个黑客行为,但也许下面的方法会奏效

obj.expects(:do_something).with(:apples).times(0)
Mocha将使用设置基数实例变量。

这里有一个解决方法,使用:

要求“测试/单元”
需要“摩卡咖啡/设置”
类MyTest
结果:

>> ruby mocha_test.rb 
Run options: 

# Running tests:

F

Finished tests in 0.000799s, 1251.6240 tests/s, 0.0000 assertions/s.

  1) Failure:
test_something(MyTest) [mocha_test.rb:10]:
unexpected invocation: #<Mock:0xca0e68>.blah(:apple)
unsatisfied expectations:
- expected never, invoked once: #<Mock:0xca0e68>.blah(:apple)
satisfied expectations:
- allowed any number of times, invoked once: #<Mock:0xca0e68>.blah(Not(:apple))


1 tests, 0 assertions, 1 failures, 0 errors, 0 skips
ruby mocha_test.rb 运行选项: #运行测试: F 以0.000799s、1251.6240次测试/秒、0.0000次断言/秒的速度完成测试。 1) 失败: 测试某物(MyTest)[mocha\u test.rb:10]: 意外调用:#.blah(:apple) 未满足的期望: -预期从不,调用一次:#.blah(:apple) 满意的期望: -允许任意次数,调用一次:#.blah(不是(:apple)) 1个测试,0个断言,1个失败,0个错误,0个跳过

总的来说,我同意你的看法:这种行为令人沮丧,违反了最小惊喜原则。也很难将上述技巧扩展到更一般的情况,因为您必须编写一个越来越复杂的“catchall”表达式。如果你想要更直观的东西,我觉得这很好。

我对被接受的答案不感兴趣,所以我发现这项工作在我的案例中是可行的

class NeverError < StandardError; end

obj.stubs(:do_something).with(:apples).raises(NeverError)

obj.do_something(:bananas)

begin
  obj.do_something(:apples)
rescue NeverError
  assert false, "expected not to receive do_something with apples"
end
class NeverError
还有改进的余地,但这给出了要点,应该很容易修改以适应大多数场景。

您尝试过这个方法吗

鉴于此: 提供与此相同的行为: 您可以翻转逻辑以获得所需:
使用除
:apples
以外的任何东西调用
do\u something
都将通过,而
obj.do\u something(:apples)
将产生意外调用。

您需要确保如果调用do\u something(:banana),您有一个期望(:do\u something)。使用(:banana)为什么?为什么mocha“接管”了方法调用。我一模仿这样的方法调用,就会把其他事情搞砸。我不太明白——你是希望多次调用该方法而不会导致错误,还是试图断言该方法从未使用给定参数调用过?两者都有。我想声明该方法从未使用给定参数调用过,但仍然允许使用其他参数调用。啊,不幸的是,当调用
obj.do\u something(:bananas)
时,这会导致相同的意外调用错误。是的,我没有更好的主意。Mocha的源代码中有一个答案,但是只要
期望(:dou_something)。用(:bananas)
尼斯就可以了。这让我绕过了这个问题,但是的,当然一点也不优雅!
>> ruby mocha_test.rb 
Run options: 

# Running tests:

F

Finished tests in 0.000799s, 1251.6240 tests/s, 0.0000 assertions/s.

  1) Failure:
test_something(MyTest) [mocha_test.rb:10]:
unexpected invocation: #<Mock:0xca0e68>.blah(:apple)
unsatisfied expectations:
- expected never, invoked once: #<Mock:0xca0e68>.blah(:apple)
satisfied expectations:
- allowed any number of times, invoked once: #<Mock:0xca0e68>.blah(Not(:apple))


1 tests, 0 assertions, 1 failures, 0 errors, 0 skips
class NeverError < StandardError; end

obj.stubs(:do_something).with(:apples).raises(NeverError)

obj.do_something(:bananas)

begin
  obj.do_something(:apples)
rescue NeverError
  assert false, "expected not to receive do_something with apples"
end
obj.stubs(:do_something).with(:apples)
obj.stubs(:do_something).with { |p| p == :apples }
obj.stubs(:do_something).with { |p| p != :apples }