Posts

Showing posts from July, 2018

[Unit Testing] Getting Started with Unit Testing

Image
Unit Testing: Is a software development process in which the smallest  testable parts of an application are individually and independently tested . Simply verifies that individual units of code (mostly functions) work as expected. Is a component of test-driven development (TDD). Usually performed by the developer. Performed using White Box Testing method. Is meant to give the developer more confidence  and trust  in the code. Encourages modularity - write programs that do one thing, and do it well! (Single Responsibility Principal) Involves faking dependencies and methods in order to isolate the scope of the System Under Test (SUT) Simple Example The most simple example is testing the result of a method is within our expectation. For example, we we have a combineNumbers method below: To test this, we simply compare the result of this method with our expected  result. When you run the tests, you will be informed that these tests have passed. When to do Unit

[Unit Testing] Test Doubles (Stubs, Mocks....etc)

Image
In unit testing, isolation is key. The goal of unit testing is to test individual components, and not an entire system . The class/object/function you are testing is System Under Test (SUT) , and the other components of the system are Collaborators or Dependencies. Test Double  is a generic term for any kind of 'pretend' object used in place of a real object for testing purposes.  There are several types of Testing Doubles. We will start with: Fake Stub Mock Command Queries Mocks vs Stubs And then: Dummy Spies Spies vs Mocks I) Basic Testing Doubles a) Fake Fake are objects that have working implementations, but not same as production one. Usually they take some shortcut and have simplified version of production code. Note that Fake is a generic term - that can point to anything; usually mock and stubs. An example of this is an in-memory implementation of a database. This fake implementation will not access the actual database, but will u