What are the advantages of Mocha over RSpec's built in mocking framework?
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)
with Mocha that becomes:
Car.any_instance.stubs(:speed).returns(100)
I find the Mocha version clearer and more explicit.
Is there a way to undo Mocha stubbing of any_instance?
You need to manually configure RSpec.
Rspec.configure do |config|
...
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# config.mock_with :rspec
end
Also, remember that Rspec provides its own methods to mock an object. Use the RSpec API or you won't be able to benefit from library abstraction.
http://rspec.info/documentation/mocks/message_expectations.html
How to mock an instance method of an already mocked object?
You could try something like this :-
user = Factory(:user)
user.stubs(:facebook => stub(:me => stub(:info => {:name => "John Doe"})))
If you really want to check that all these methods are called (which I suspect you don't), you could do the following :-
user = Factory(:user)
user.expects(:facebook => mock(:me => mock(:info => {:name => "John Doe"})))
It's a bit more verbose, but it's usually worthwhile giving each mock object a name :-
user = Factory(:user)
user.stubs(:facebook => stub('facebook', :me => stub('me', :info => {:name => "John Doe"})))
I hope that helps.
What's the difference between a mock & stub?
Stub
I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They would not do anything fancy and you would have already written the stubbed code for it outside of your test.
Mock
A mock is something that as part of your test you have to setup with your expectations. A mock is not setup in a predetermined way so you have code that does it in your test. Mocks in a way are determined at runtime since the code that sets the expectations has to run before they do anything.
Difference between Mocks and Stubs
Tests written with mocks usually follow an initialize -> set expectations -> exercise -> verify
pattern to testing. While the pre-written stub would follow an initialize -> exercise -> verify
.
Similarity between Mocks and Stubs
The purpose of both is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler in what they are trying to prove.
Related Topics
How to Organize Minitest/Unit Tests
How to Validate the Presence of a Belongs to Association with Rails
Write and Read a File with Utf-8 Encoding
Differencebetween "Be_True" and "Be True" in Rspec
Why Is Devise Not Displaying Authentication Errors on Sign in Page
How to Run Irb.Start in Context of Current Class
Sudo Gem Install Pg Won't Work
Get the Name of a Local Variable
Change Default Capybara Browser Window Size
Get Text Directly Inside a Tag in Nokogiri
Ruby: How to Group a Ruby Array
Circular Dependency Detected While Autoloading Constant When Loading Constant
Rails/Rspec: How to Test #Initialize Method
How to Create Md5 Hash with Hmac Module in Ruby
Do You Check in Your Rvmrc File
Testing a Concern/Module That Uses Activerecord
In Ruby, How to I Control the Order in Which Test::Unit Tests Are Run