Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Angular's Dependency Injection Is Pretty Awesome For Testing

2/22/2019

0 Comments

 
As a professional software developer I have had the luxury of being able to work on successful professional projects in AngularJS, React, and now the modern Angular. To me it's so funny how similar the two frameworks are in terms of the problem they are trying to solve and even the way they do it.

A Tale of Non-DI

It wasn't until I worked on a front-end javascript project which did not use dependency injection that I  really missed and appreciated it. At the time I was working on a React project, and the team values were to just stay true to regular, vanilla es6 JavaScript. We still had "services", ie helper classes that contained a few functions which did some business logic. However, we we doing a lot of instantiation. We were manually calling "new X()" in the code, or the class would call it right before module.exports, etc. Sure, it's true that coding it out in this way works, but then when you go to test it and you have MyComponent which uses MyService it's difficult to "reach into" MyService and tell it that instead of MyServiceDepencency it should use MyServiceDepencencyMock. You may even wish to not Mock MyServiceDependency, but actually it's a dependency of MyServiceDependency. To me it feels like you need to hackishly change your code to make it easier to mock things, and while it's totally possible I just feel like it's not as fun and fluid as the DI model in Angular.

Mock Anything, Anywhere

Angular's dependency injection is great because it allows you to say "For any of my services anywhere using JimHttp, just use JimHttpMock instead". All you need to do is put an object for it in the providers array when setting up your test, as in the example below:
let component: HelloComponent;
let userService: UserService;
 
beforeEach(() => {
  
  TestBed.configureTestingModule({
    declarations: [
      HelloComponent
    ],
    providers: [
      {provide: UserService, useClass: MockUser}
    ]
  });
  
  component = TestBed.createComponent(HelloComponent).componentInstance;

  // use HelloComponent with MockUser in place of UserService
});
By allowing a framework to create and instantiate dependencies we don't have to put all that in our code, and after injecting a service we just immediately start using it. This makes everything super decoupled and allows us to easily substitute in Mocks or spy on functions without even needing to know the "compositional inheritance chain" of how exactly how that asynchronous side effect class is related to each individual component or service.

Jasmine

Angular comes with jasmine out of the box as the framework used for unit testing. Personally, I like the jasmine syntax much better than mocha / chai, especially the syntax for spying on a function, etc. so for me it's a win that Angular uses jasmine, but I do know some people who don't like the syntax.

What Do You Think?

I was an AngularJS developer and then I switched over to React, but now I think I'm liking new Angular even better, simply because of the DI and how much easier it makes testing. haha. What do you guys think? Are you liking testing in Angular as well? Are you a React dev who is totally fine testing without dependency injection? Let me know! ?
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletters??
    Sign up here:

    - Triple Gainers
    - Rippers N' Dippers
    - Growingest Growers
    ​

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021