Come to my special Film Festival shop at WORM! Free entry from 17:00 to 21:30. After that you’ll need a ticket or guest pass for the sound//vision nights which go on till very late. (click here) I’ll present a great collection of underground and avant-garde films on DVD, VHS and floppy disc, plus several books and magazines about film culture in ALL its aspects! On the side, some nice new records and tapes to complete the picture.
Wipro is proud to be a Platinum sponsor of SAP Hybris LIVE: Global Summit, October 17-19, 2017 in Barcelona, Spain.
This event is a unique learning opportunity for marketing, sales, commerce, service, revenue and IT leaders and professionals. Spend two days taking a deep dive into the business and technology of customer engagement and commerce. Learn what it takes to deliver great experiences, simplify your processes and transform your business.
Delve into the technologies and processes that are revolutionizing the front office, like machine learning, artificial intelligence, microservices and IoT with our product and implementation experts, and get keen insights into what’s coming next. Thought leaders and industry influencers from across the globe will share their predictions about the trends that will shape your marketplace for years to come.
Q: Angular 9 Unit Test: how to mock import of tested component? So I'm currently writing a Jasmine/Karma Unit Test for an Angular 9 Component.
A short summary how my application works:
I've written a little Funnel with D3 that displays given data in a funnel-like diagram. Then I've written a FunnelComponent that contains this Funnel and also displays some meta information next to the actual diagram.
This is my to be tested Component:
funnel.component.ts
import { Component } from '@angular/core';
import { Funnel } from './d3charts/funnel.ts';
import { FunnelData } from './funnel.data';

@Component({
  selector: 'v7-funnel-component',
  templateUrl: './funnel.html'
})
export class FunnelComponent {

  private funnel: Funnel = null;

  constructor() {}

  public createFunnel(funnelData: FunnelData): void {
      this.funnel = new Funnel();
      this.funnel.setData(funnelData);
      this.funnel.draw();
  }
}

And here is my karma-jasmine unit-test for that Component:
funnel.component.spec.ts
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { } from 'jasmine';
import { FunnelComponent } from './funnel.component';
import { Component } from '@angular/core';
import { FunnelData } from './funnel.data';
import { Funnel } from './d3charts/funnel.ts';

@Component({selector: 'funnel', template: ''})
class FunnelStub {
  private data: FunnelData = null;

  public setData(data: FunnelData): void
  {this.data = data;}
  {}
  public draw(): void
  {}
  public update(funnelData: FunnelData): void
  {}
}

/**
 * Testing class for FunnelComponent.
 */
describe('Component: Funnel', () => {

  let component: FunnelComponent;
  let fixture: ComponentFixture<FunnelComponent>;

  beforeEach(async(() => {
    TestBed
   .configureTestingModule({
      declarations: [
        FunnelComponent,
        FunnelStub
      ],
      providers: [
        { provide: Funnel, useValue: FunnelStub}
      ]
    })
   .compileComponents()
   .then(() => {
      fixture = TestBed.createComponent(FunnelComponent);
      component = fixture.componentInstance;
    });
  }));

it('#createFunnel should set data of funnel. Filled data should set filled funnel.', () => {
    expect(component["funnel"]).toBeNull();

    let exampleFunnelData = new FunnelData("testcaption", "testdescription", 8);
    component.createFunnel(exampleFunnelData);

    expect(component["funnel"]).toBeDefined();
    expect(component["funnel"]["data"]).toBeDefined();
    expect(component.data.caption).toEqual("testcaption");
    expect(component.data.description).toEqual("testsubtext");
    expect(component.data.value).toEqual(8);
  });
});

I want to test the createFunnel method here.
But I don't want that my createFunnel method assigns a real Funnel to this.funnel, but uses my FunnelStub instead.
Any idea how to do this?
Adding { provide: Funnel, useValue: FunnelStub} to my providers array didn't help :(
Best regards,
Sebastian

A: This does not work, because Funnel is not a provider:
providers: [
  { provide: Funnel, useValue: FunnelStub}
]

What would help you the most for testing, would be to create a service for the Funnel creation.
export abstract class FunnelProvider {
  abstract createFunnel(): Funnel;
}


This service can then have 2 implementations, one implementation you use in your app and one that you use in your tests.
@Injectable()
export class DefaultFunnelProvider {
  createFunnel() {
    return new Funnel(); // Return real funnel object.
  }
}

@Injectable()
export class MockFunnelProvider {
  createFunnel() {
     // Return stub funnel. Assuming that they share the same interface.
    return new FunnelStub() as Funnel;
  }
}

Now you have to define the DefaultFunnelProvider in your FunnelModule (the module that declares your FunnelComponent):
@NgModule({
 ... // other stuff
  providers: [{provide: FunnelProvider, useClass: DefaultFunnelProvider}]
})
export class FunnelModule {}


Then you inject the FunnelProvider in your FunnelComponent and use it to create the funnel:
export class FunnelComponent {

  private funnel: Funnel = null;

  constructor(private funnelProvider: FunnelProvider) {}

  public createFunnel(funnelData: FunnelData): void {
      this.funnel = this.funnelProvider.createFunnel();
      this.funnel.setData(funnelData);
      this.funnel.draw();
  }
}

A lot of work, but now the cool thing is that you can inject the Mock implementation of the FunnelProvider in your TestBed:
TestBed
   .configureTestingModule({
      declarations: [
        FunnelComponent,
        FunnelStub
      ],
      providers: [
        { provide: FunnelProvider, useValue: MockFunnelProvider }
      ]
    })

Now when funnelProvider.createFunnel is called, it will not return a real funnel, but your funnel stub.
If you must during your test, you can cast the components funnel field to FunnelStub (e.g. if it has some special method):
const funnelStub = component.funnel as FunnelStub;

After running tens of thousands of personal training sessions it is clear that “wanting” to get strong and in shape is one thing but “willing” is a separate issue.
The stuff of success, at least with our clients, starts and ends with knowing the power is yours to use as you see fit…no pun intended. This removes the internal and external conflicts with regards to making change start and stick.
Know that being in shape and feeling strong is not only possible but personal. It’s a choice you can avoid or have no interest in but it is yours. It is not the property of your nagging negative self nor does it belong in the hands of societal pressure.
Thought-provoking. Keep them coming!
Sorenson is a manufacturer of plastic consumables dedicated to the handling of liquids in the laboratory. Founded in 1981 in the United States, Mµlti Technology, Inc. innovates in products for liquids treatment in scientific research, allowing to gain in precision and reproducibility. It joined the Sorenson Group in 1991, specialized in the innovation of liquid treatment products for the study of molecular biology.
Sorenson BioScience, Inc. is committed to quality through strict manufacturing processes and controls, the choice of non-recycled raw materials, the traceability and the purity of its products, thus offering one of the best qualities of materials.
Dominique DUTSCHER offers you Mµlti Sorenson products for your laboratory molecular biology work: pipettors, microtubes, PCR plates, micropipettes, and a wide range of rack and bulk tips to meet all your needs (including Multiflex and Miniflex gel tips).
Jak II The Untold Version Ch.2Ch.2: TrainingAfter