Mock工具: Mocha还是FlexMock

简介

Mocha和FlexMock是两个比较流行的针对Rails单元测试的Mock工具,

Mocha:
作者:JamesBen, Chris 以及 Paul
许可:MIT License

SVN:svn://rubyforge.org/var/svn/mocha/trunk
文档:http://mocha.rubyforge.org/

FlexMock:
作者:Jim Weirich
许可:类BSD License
SVN:svn://rubyforge.org/var/svn/mocha/trunk

文档:flexmock.rubyforge.org

安装

Mocha:

$ gem install mocha
$ script/plugin install svn://rubyforge.org/var/svn/mocha/trunk

FlexMock:

$ gem install flexmock

使用

Mocha:

# Mocking a class method
product = Product.new
Product.expects(:find).with(1).returns(product)
assert_equal product, Product.find(1)

FlexMock:

product = Product.new
flexmock(Product).should_receive(:find).with(1).and_return(product)
assert_equal product, Product.find(1)

这里只是一个简单的比较,SlideShare上专门有一个幻灯对Mocha和FlexMock的方方面面进行了比较,最后的结论是,两者的功能基本相同,差异只在于语法,所以不必担心选择一个会失掉另一个的某些功能,喜欢哪个语法就用哪个好了,我比较喜欢Mocha的语法。

This entry was posted in 测试. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Zhonghai Zuo

    Good article. Any ideas for the Mocks/Stubs from RSpec? Keep up the good work!

  • admin

    Mocha and FlexMock also declared that they support RSpec, but I didn’t try this yet.

  • jeremy

    the only RoR focused blog in Chinese language that is practically useful :)

    thanks for all the info and please keep on your good work!

    pity though the blog itself is not running on Mephisto/Typo.

无觅相关文章插件,快速提升流量