evangalen / ng-improved-testing

Improves AngularJS testing
MIT License
21 stars 4 forks source link

Mocking out controller with dependencies coming from different modules? #4

Closed milesburton closed 10 years ago

milesburton commented 10 years ago

Hi,

I've been having a look through the code for your library and it's all looking very positive. My question is about modular builds.

As I understand you can define a module (A) and mock an item (C) and related dependencies (D). The problem lies when you have a dependency which sits in another module.

Now I'm guessing Angular doesn't actually care where D is injected from so it should be possible to just declare/mock a module and allow ng-improved to provide the mocks as if they came from A.

Could you clarify?

evangalen commented 10 years ago

Sorry for my very late response... I have been away on holidays for the last 2 weeks.

The instances of the (to be mocked) dependencies of a (to be tested) component (i.e. a controller using a be mocked dependency service) will be retrieved using an AngularJS injector instance (https://docs.angularjs.org/api/auto/service/$injector). And by using the AngularJS injector the ngImprovedTesting code isn't aware in which module a dependency service is actually declared in order to mock a dependency.

However ngImprovedTesting does need to know how the component being tested (i.e. a service for which dependencies need to be mocked) was declared. This because it creates new AngularJS module with altered (annotated) component declararations that uses mocked out services (registered with a "Mock" post-fix by ngImprovedTesting) instead of the actual service.

Hope this answer your question. Let me know when you have additional questions.