0

For better testing and separating frontend / backend development we are currently trying to find a dynamic way to provide mock service implementations in devMode.

The idea is to specify in your environment which services shall be mocked and have the injector then return either the real or the mocked implementation based on this.

The question is now if it is possible to dynamically do this for every service the injector provides or if we have to use a separate factory (doing the same thing) for every service?

// pseudo code
onRequestProviderFor(serviceName)
{
  if(environment.mockTheseServices.contains(serviceName))
  {
    return new "Mock"+serviceName.ts
  }
  else
  {
    return new serviceName.ts
  }
}

Is this possible? And is there a hook in the injector process we could use to do this (in devMode)?

9
  • I think you need a separate factory for every service. You can do some code generation, that updates barrel files so that the same imports lead to different classes. Commented Jul 19, 2017 at 6:57
  • You can add custom providers when you bootstrap the app, have you tried that? Commented Jul 19, 2017 at 6:58
  • do you intend to have that code environment.mockTheseServices.contains(serviceName) in production? Commented Jul 19, 2017 at 6:59
  • @GünterZöchbauer I was afraid so, but thanks for confirming. Commented Jul 19, 2017 at 7:04
  • @jonrsharpe I was looking for the cleanest way to do this dynamically for all services, not just for one (unless I misunderstood you). Commented Jul 19, 2017 at 7:05

1 Answer 1

1

Did you consider creating a separate mock server using something like https://github.com/typicode/json-server ?

I think it would keep your project code cleaner to not include mock services in the same code base.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.