6.2.3 • Published 7 months ago

ng-auto-moq v6.2.3

Weekly downloads
70
License
MIT
Repository
github
Last release
7 months ago

Build Status NPM version:latest NPM version:next npm downloads Dependency Status Commitizen friendly semantic-release Renovate enabled npm bundle size (minified + gzip) License

This is a special angular injector builder for unit testing purposes. It creates an injector configuration that automatically mocks all dependencies of tested unit with moq.ts.

It can be used with angular TestBed class and with a regular injector.

Here is adapted test configuration example from the official angular documentation.

import "reflect-metadata";
import { moqInjectorProviders, IMockedObject, resolveMock } from "ng-auto-moq";

@Injectable()
export class MasterService {
  constructor(private valueService: ValueService) { }
  getValue() { return this.valueService.getValue(); }
}

@Injectable()
export class ValueService {
  getValue() { return 10; }
}

let masterService: MasterService;
let valueServiceMocked: ValueService;

beforeEach(() => {

  TestBed.configureTestingModule({
    // Provide both the service-to-test and its (moq) dependency
    providers: moqInjectorProviders(MasterService)
  });
  // Inject both the service-to-test and its (spy) dependency
  masterService = TestBed.get(MasterService);
  valueServiceMocked = TestBed.get(ValueService);
  
  (valueServiceMocked as IMockedObject<ValueService>).__mock
  .setup(instance => instance.getValue())
  .returns(-1);
  
  //or
  
  resolveMock<ValueService>(ValueService, TestBed.get(Injector)) 
    .setup(instance => instance.getValue())
    .returns(-1);
});

Services testing could be also done in simplified manner with a regular injector. With this approach tests could be performed in nodejs environment without involving a browser. It gives performance improvement and less dependencies should be install, suits well for CI environment. If you are using jasmine just run it from command prompt with the following command:

jasmine *.spec.js
import "reflect-metadata";
import { moqInjectorProviders, IMockedObject, resolveMock } from "ng-auto-moq";

let injector: Injector;

beforeEach(() => {
    injector = Injector.create(moqInjectorProviders(MasterService));
});

it("Returns provided value", ()=>{
    // setup section
    resolveMock<ValueService>(ValueService, injector)
      .setup(instance => instance.getValue())
      .returns(-1);
    
    //action section
    const tested = injector.get(MasterService);
    const actual = tested.getValue();
    
    //assertion section
    expect(actual).toBe(-1);
})

With options of moqInjectorProviders you can control how dependencies are configured. Let's say you have a class with @Optional dependency, and you want to test both cases when the dependency is available and when it isn't.

import "reflect-metadata";
import { moqInjectorProviders, DefaultProviderResolver, IParameter, ProviderResolver } from "ng-auto-moq";

@Injectable()
export class MasterService {
  constructor(@Optional() private valueService: ValueService) { }
  getValue() { return this.valueService ? this.valueService.getValue() : -1; }
}

it("Returns provided value when optional dependencies are not available", ()=>{
    // setup section
    const providerResolver: ProviderResolver = (parameter: IParameter, mocked: Type<any>, defaultProviderResolver: DefaultProviderResolver)=>{
        if (parameter.optional === true){
            return undefined;
        }
        return defaultProviderResolver(parameter, mocked);
    };
    
    const injector = Injector.create(moqInjectorProviders(MasterService, { providerResolver }));
    
    //action section
    const tested = injector.get(MasterService);
    const actual = tested.getValue();
    
    //assertion section
    expect(actual).toBe(-1);
})

Another option is mockFactory that allows to customize the dependency mocking process. By default the mocked dependencies are preconfigured with only one property: __mock that provide access to IMock interface. You can either decide to throw an exception when interaction with the mocked object is not expected.

import "reflect-metadata";
import { moqInjectorProviders, MockFactory, DefaultMockFactory, IParameter } from "ng-auto-moq";
import { It } from "moq.ts";

let injector: Injector;

beforeEach(() => {
    const mockFactory: MockFactory = (parameter: IParameter, defaultMockFactory: MockFactory<any>) =>{
        const mock = defaultMockFactory(parameter);
        mock
        .setup(instance => It.Is(expression => true))
        .throws(new Error("setup is missed"));
        return mock;
    };
    injector = Injector.create(moqInjectorProviders(MasterService, {mockFactory}));
});

it("Throws an exception", ()=>{
    //action section
    const tested = injector.get(MasterService);
    
    //assertion section
    expect(()=> tested.getValue()).toThrow();
})
6.2.4

7 months ago

6.2.3

11 months ago

6.2.2

2 years ago

6.1.0

2 years ago

6.0.0

2 years ago

6.2.1

2 years ago

6.2.0

2 years ago

5.3.0

2 years ago

5.2.0

3 years ago

5.1.0

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.1.0

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.1

4 years ago

4.0.2

4 years ago

4.0.0

4 years ago

3.1.0

4 years ago

3.0.8

5 years ago

3.0.7

5 years ago

3.0.6

5 years ago

3.0.5

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago

1.0.1-rc.1

5 years ago