Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to do simple dependency injection #1458

Open
fatso83 opened this issue Jun 15, 2017 · 5 comments
Open

Document how to do simple dependency injection #1458

fatso83 opened this issue Jun 15, 2017 · 5 comments

Comments

@fatso83
Copy link
Contributor

fatso83 commented Jun 15, 2017

There are usually three main categories of stubbing when dealing with network requests:

  1. stubbing out the api doing the actual network calls using a link seam
  2. stubbing out the api doing the actual network calls using dependency injection of some sort
  3. stubbing out the network layer in the underlying engine (browser or Node)

Presently we only have one article covering the first option, and very little on the two next approaches. We should create a small how-to for the article section that covers how to do basic (no framework) dependency injection.

I have already written a lot of text that could serve as the basis for such an article.

@fatso83 fatso83 assigned fatso83 and unassigned fatso83 Jun 15, 2017
@fatso83 fatso83 self-assigned this Jun 15, 2017
@fatso83 fatso83 changed the title How to do simple dependency injection for testing Document how to do simple dependency injection Jun 15, 2017
@fatso83
Copy link
Contributor Author

fatso83 commented Jun 15, 2017

I think it could be nice to try and attack the same module using the three different approaches in three (inter-linked) articles so that user's could see how they compared. Maybe even putting sample code along with the tests in the repo so people could clone and run it as well. #foodforthought

@FranceJ
Copy link

FranceJ commented Oct 6, 2020

Hi folks, I've used DI and Sinon together a bit in order to stub method dependencies. I would be willing to have a crack at this if it still needs doing.

Is this meant to be a doc on DI in general, or on using DI with Sinon to stub out dependencies?

@mroderick
Copy link
Member

@FranceJ I'd be happy to have an article that showed how to do this using Sinon for the dependencies.

@faithk7
Copy link

faithk7 commented Oct 23, 2022

Should this issue be closed, given there is a relevant documentation: https://sinonjs.org/how-to/stub-dependency/?

@fatso83
Copy link
Contributor Author

fatso83 commented Oct 31, 2022

@faithk7 No, that is not using dependency injection. That is employing link seams / module level loader interception.

DI does not require any changes to the runtime. See the links I added at the top of this where I show how to allow substituting dependencies at runtime without any magic/additional complexity. This approach works regardless of using TypeScript, different JS runtimes (browser, Node, Deno) or transpiling to some other target level without needing to deep dive to get it working. Link seams are always dependant on the runtime, which means one approach used for Node will not work if transpiling using Webpack 3 which again will be different from Webpack 4, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants