@jeremyy7 wrote:
Can someone provide me with an example of how to mock the Storage module and implement it? I am simply attempting to test a redirect but because the component uses Storage, I keep seeing an error:
Failed: StaticInjectorError(DynamicTestModule)[Storage]: StaticInjectorError(Platform: core)[Storage]: NullInjectorError: No provider for Storage!
So far my test looks as follows:
describe('AppComponent', () => { let statusBarSpy, splashScreenSpy, platformReadySpy, platformSpy; let router: Router; let location: Location; let fixture; beforeEach(async(() => { statusBarSpy = jasmine.createSpyObj('StatusBar', ['styleDefault']); splashScreenSpy = jasmine.createSpyObj('SplashScreen', ['hide']); platformReadySpy = Promise.resolve(); platformSpy = jasmine.createSpyObj('Platform', { ready: platformReadySpy }); TestBed.configureTestingModule({ declarations: [AppComponent], imports: [ RouterTestingModule.withRoutes(routes), HttpClientTestingModule, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], providers: [ { provide: StatusBar, useValue: statusBarSpy }, { provide: SplashScreen, useValue: splashScreenSpy }, { provide: Platform, useValue: platformSpy }, ], }).compileComponents(); router = TestBed.get(Router); location = TestBed.get(Location); fixture = TestBed.createComponent(AppComponent); router.initialNavigation(); })); it('navigates to "" redirects you to /login', fakeAsync(() => { router.navigate(['']); tick(); expect(location.path()).toBe('/login') }));
I am pretty new to testing in Angular so any point in the right direction would be very helpful.
Posts: 1
Participants: 1