2013-03-06 48 views
0

我正在尝试使用flexmock测试/单元为我的单元测试创​​建模拟对象。这是我在测试类:使用flexmock进行单元测试不会如预期的那样失败

class Cave 
    def hunt 
    # not yet implemented 
    end 
end 

这是我的单元测试(请注意,此方法只在测试情况下,许多方法中的一种):

require 'test/unit' 
require 'flexmock/test_unit' 
require 'cave' 

def test_play 
    hunter = flexmock() 

    cave = Cave.new 
    cave.hunter = hunter 

    hunter.should_receive(:turn).with(FlexMock.any).at_least.once 

    cave.hunt 
end 

这个测试应该验证hunt方法将turn消息至少发送给分配给cavehunter属性的对象至少一次。

如果我正确理解文档,flexmock与单元测试框架绑定并自动验证在测试中定义的期望。因此,我期望上述测试失败,因为方法hunt尚未发送turn。但是,测试成功。

我错过了什么吗?

更新:我建立了一个新文件,其中只包含一个最小测试,以查看flex mock是否能像预期的那样工作。下面的测试产生了预期的结果,即失败:

require 'test/unit' 
require 'flexmock/test_unit' 

class TestFoo < Test::Unit::TestCase 
    def test_foo 
    m=flexmock() 
    m.should_receive(:bar).and_return(17).once 
    end 
end 

回答

0

我认为你的设置有问题!

的文件和你的例子之后,我得到下面的输出:

ruby test_foo.rb 
Run options: 

# Running tests: 

[1/1] TestFoo#test_foo = 0.00 s 
    1) Failure: 
test_foo(TestFoo) [/Users/paule/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/flexmock-1.3.1/lib/flexmock/test_unit_integration.rb:53]: 
in mock 'unknown': Method 'bar(*args)' called incorrect number of times 
1 matching call expected 
0 matching calls found 
No messages have been received 


Finished tests in 0.011163s, 89.5817 tests/s, 0.0000 assertions/s. 
1 tests, 0 assertions, 1 failures, 0 errors, 0 skips 

ruby -v: ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.1] 

这表明一切使用红宝石2.0.0p0运行正常。

+0

我相信你是对的 - 在另一个测试案例中这是有效的。我不知道有什么区别,但不值得努力找出,因为无论如何我都会切换到rspec。谢谢! – waldrumpus 2013-03-18 11:21:54

+0

当flexmock [没有在minitest内运行](https://github.com/jimweirich/flexmock/issues/14)时,我从flexmock切换到[rr](https://github.com/rr/rr/) 。 – zhon 2013-10-05 19:17:32