How to use TOKEN_CLASS_MOCK method in ng-mocks

Best JavaScript code snippet using ng-mocks

test.spec.ts

Source:test.spec.ts Github

copy

Full Screen

1import { Injectable, InjectionToken, NgModule } from '@angular/core';2import { MockBuilder, ngMocks } from 'ng-mocks';3@Injectable()4class Class1Service {5 public constructor(public readonly name: string) {}6}7@Injectable()8class Class2Service {9 public readonly name = 'class2';10}11const TOKEN_CLASS_MOCK = new InjectionToken('MOCK');12const TOKEN_CLASS_KEEP = new InjectionToken('KEEP');13@NgModule({14 providers: [15 {16 provide: 'name',17 useValue: 'useValue',18 },19 {20 deps: ['name'],21 provide: Class1Service,22 useClass: Class1Service,23 },24 Class2Service,25 {26 deps: ['name'],27 provide: TOKEN_CLASS_MOCK,28 useClass: Class1Service,29 },30 {31 deps: ['name'],32 provide: TOKEN_CLASS_KEEP,33 useClass: Class2Service,34 },35 ],36})37class TargetModule {}38// fix for jest without jasmine assertions39const assertion: any =40 typeof jasmine === 'undefined' ? expect : jasmine;41// useClass ignores existing services with the same class name, but42// ng-mocks still detects whether the class should be replaced with a mock copy or not.43describe('tokens-class', () => {44 ngMocks.faster();45 beforeEach(() =>46 MockBuilder().mock(TargetModule).keep(Class2Service),47 );48 it('resolves Class1Service as a mock instance', () => {49 const actual = ngMocks.findInstance(Class1Service);50 expect(actual).toEqual(assertion.any(Class1Service));51 expect(actual.name).toBeUndefined();52 });53 it('resolves Class2Service as a real instance', () => {54 const actual = ngMocks.findInstance(Class2Service);55 expect(actual).toEqual(assertion.any(Class2Service));56 expect(actual.name).toEqual('class2');57 });58 it('resolves TOKEN_EXISTING_MOCK as a mock instance', () => {59 const actual = ngMocks.findInstance<any>(TOKEN_CLASS_MOCK);60 expect(actual).toEqual(assertion.any(Class1Service));61 expect(actual.name).toBeUndefined();62 });63 it('resolves TOKEN_EXISTING_KEEP as a real instance', () => {64 const actual = ngMocks.findInstance<any>(TOKEN_CLASS_KEEP);65 expect(actual).toEqual(assertion.any(Class2Service));66 expect(actual.name).toEqual('class2');67 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { TOKEN_CLASS_MOCK } from 'ng-mocks';2import { MyService } from './my.service';3import { MyComponent } from './my.component';4describe('MyComponent', () => {5 let component: MyComponent;6 let service: MyService;7 beforeEach(() => {8 TestBed.configureTestingModule({9 {10 useFactory: () => ({11 myMethod: () => 'a',12 }),13 },14 });15 component = TestBed.inject(MyComponent);16 service = TestBed.inject(MyService);17 });18 it('should work', () => {19 spyOn(service, 'myMethod').and.returnValue('b');20 expect(component.myMethod()).toEqual('a');21 });22});23import { Injectable } from '@angular/core';24@Injectable()25export class MyService {26 myMethod() {27 return 'a';28 }29}30import { Component } from '@angular/core';31import { MyService } from './my.service';32@Component({33})34export class MyComponent {35 constructor(private readonly service: MyService) {}36 myMethod() {37 return this.service.myMethod();38 }39}40import { TOKEN_CLASS_MOCK } from 'ng-mocks';41import { MyService } from './my.service';42import { MyComponent } from './my.component';43describe('MyComponent', () => {44 let component: MyComponent;45 let service: MyService;46 beforeEach(() => {47 TestBed.configureTestingModule({48 {49 useFactory: () => ({50 myMethod: () => 'a',51 }),52 },53 });54 component = TestBed.inject(MyComponent);55 service = TestBed.inject(MyService);56 });57 it('should work', () => {58 spyOn(service, 'myMethod').and.returnValue('b');59 expect(component.myMethod()).toEqual('a');60 });61});62import { Injectable } from '@angular/core';63@Injectable()64export class MyService {65 myMethod() {66 return 'a';67 }68}69import { Component } from '@angular/core';70import { MyService } from './my.service';71@Component({

Full Screen

Using AI Code Generation

copy

Full Screen

1import { TOKEN_CLASS_MOCK } from 'ng-mocks';2describe('TestComponent', () => {3 beforeEach(() => {4 TestBed.configureTestingModule({5 {6 useValue: {7 method: () => 'mocked',8 },9 },10 });11 });12 it('should create', () => {13 const fixture = TestBed.createComponent(TestComponent);14 fixture.detectChanges();15 expect(fixture.componentInstance).toBeTruthy();16 });17});18import { TOKEN_CLASS_MOCK } from 'ng-mocks';19describe('TestComponent', () => {20 beforeEach(() => {21 TestBed.configureTestingModule({22 {23 useValue: {24 method: () => 'mocked',25 },26 },27 });28 });29 it('should create', () => {30 const fixture = TestBed.createComponent(TestComponent);31 fixture.detectChanges();32 expect(fixture.componentInstance).toBeTruthy();33 });34});35import { TOKEN_CLASS_MOCK } from 'ng-mocks';36describe('TestComponent', () =>

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ng-mocks automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful