What are the advantages of Mocha over RSpec’s built in mocking framework? [closed]

Ruby mocking frameworks have evolved a lot since this question has been asked in 2009. So here is a little 2013 comparison:

Expectations

  • with Rspec-mocks: expect(user).to receive(:say_hello)
  • with Mocha: user.expects(:say_hello).once

Stubbing an object

  • with Rspec-mocks: user = double(name: 'John Doe')
  • with Mocha: user = stub(name: 'John Doe')

Stubbing anything

  • with Rspec-mocks: User.any_instance.stub(:name).and_return('John Doe')
  • with Mocha: User.any_instance.stubs(:name).returns('John Doe')

They offer the same facilities, and both can be used with or without Rspec.
So I would say choosing one over another is a matter of personal taste (and they taste quite alike).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)