Who

DRAFT

When is a Dependency Not? 

Test Double Pattern Image

Let’s cut to the chase. You wouldn’t use a test-double pattern to swap the Math.min behavior for a test.  Math.min is an implementation detail, not a dependency. 

You shouldn’t use a test-double pattern to swap any behaviors that are implementation details, regardless of which class/file/repository they’re in. 

In Angular, just about everything is “dependency injected” - implying just about everything is a dependency.  Dependency-injected classes don’t have to be swapped at test time. 

They shouldn’t be swapped when they’re implementation details. 

Don’t build unit tests for classes. Don’t feel constrained because a dozen years ago some tester declared that a class is a unit. Build your unit tests for units of functionality. 

wiki.c2.com: SingletonGlobalProblems

They’re right, and they knew that before Angular was even a thing. 

Here’s another thing someone said once:

Reasons to mock:

  1. real object has non-deterministic behavior
  2. real object is difficult to set up
  3. real object has behavior that is hard to cause (e.g., network error) (similar to 1)
  4. real object is slow
  5. real object has (or is) a UI
  6. test needs to query the object, but the queries are not available in the real object (e.g., “was this callback called?”) (This needs a MockObject that has more stuff than the real object; the other cases usually require a stub that is far smaller than the real object). 
  7. real object acts “normal” most of the time, but once a month (or even less often) it does something “exceptional”. We want UnitTests to make sure the rest of the system does the RightThing whether or not the object is acting “normal” or “exceptional”. (Is this the same as #2 ?) 
  8. real object does not yet exist

wiki.c2.com: MockObject

That is all.