When to mock and How to mock

What is mock ? A mock is an object which we can use on behalf of another object. In short, mocking is creating (fake) objects that simulate the behavior of Real Objects. An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to test you replace the other objects by mocks that simulate the behavior of the real objects. When to mock ? Mocking is required when real objects are impractical to incorporate into the unit test. Most of the time external services or methods we have to mock. Once mocked, the

Stub HTTP requests in Rspec

In rspec, we need to make http call for our test to pass. In this case, we are actually making http request which is not a best practice while we are executing tests. The best way is to fake http request with the actual expected response without making actually going to that url. For faking http request in ruby, we have couple of gems namely FakeWeb and WebMock. FakeWeb: Usage: Include 'fakeweb' in your Gemfile under test group only. require it in spec helper. use it in specs Syntax: FakeWeb.register_uri(method, url, :body => "response text, html,