aurelia / documentation

The documentation for Aurelia.
MIT License
105 stars 111 forks source link

Testing > DI dependencies #445

Open baerrach opened 4 years ago

baerrach commented 4 years ago

https://aurelia.io/docs/testing/components#using-a-real-parent-view-model

In "A Custom Attribute Test with Real Parent View-model and DI dependencies"

The example is

  import {StageComponent} from 'aurelia-testing';
  import {bootstrap} from 'aurelia-bootstrapper';
  import {MyComponent} from 'src/my-component';
  import {Container} from 'aurelia-dependency-injection';
  import {MyService} from 'src/my-service';

  describe('MyAttribute', () => {
    let component;
    let container;
    let viewModel;
    let myService;

    beforeEach(() => {
      container = new Container();
      myService = container.get(MyService);
      viewModel = container.get(MyComponent);
      component = StageComponent
          .withResources('src/my-attribute')
          .inView('<div my-attribute.bind="color">Bob</div>')
          .boundTo(viewModel);
    });
    //...
  });

Shouldn't the .get calls be .registerInstance instead? How does StageComponent know about the newly created Container?

EisenbergEffect commented 4 years ago

The container will auto-register services if you call get and there’s not a prior registration. However, the fact that the testing helper is able to access them from a container instance that doesn’t appear to be related is odd. I know how this is working (it’s kind of a bad design we have in the framework that we’ve eliminated for vNext) but that doesn’t make it particularly easy to understand.

@bigopon @zewa666 What do you guys think? Can we make some improvement here? Intuitively, one would expect the StageComponent API to receive an instance of the custom container. I can’t recall if we have a way to do that or not though. If we do, it would probably be clearer if we updated this example to use that.