与RSpec的内置模拟框架相比,Mocha有何优势? [英] What are the advantages of Mocha over RSpec's built in mocking framework?

查看:68
本文介绍了与RSpec的内置模拟框架相比,Mocha有何优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,许多人比RSpec的内置模拟框架更喜欢Mocha.有人可以解释Mocha或其他替代方法相对于RSpec在模拟框架中内置的优势吗?

I notice that a lot of people prefer Mocha over RSpec's built in mocking framework. Can someone explain the advantages of Mocha, or any alternative, over RSpec's built in a mocking framework?

推荐答案

我真正喜欢的一个特定功能是能够存出类的所有实例.很多时候,我使用RSpec模拟执行以下操作:

One specific feature I really like is being able to stub out all instances of a class. A lot of times I do something like the following with RSpec mocks:

stub_car = mock(Car)
stub_car.stub!(:speed).and_return(100)
Car.stub!(:new).and_return(stub_car)

摩卡咖啡变成:

Car.any_instance.stubs(:speed).returns(100)

我发现Mocha版本更清晰,更明确.

I find the Mocha version clearer and more explicit.

这篇关于与RSpec的内置模拟框架相比,Mocha有何优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆