Ionic Storage service test is failing : Argument of type 'string' is not assignable to parameter of type 'Expected<Promise<any>>'

Viewed 18

I've been trying to implement some tests for an angular/ionic application and whenever I run ng-test I get an error of "Argument of type 'string' is not assignable to parameter of type 'Expected<Promise>'." and I'm kind of unsure of where I'm going wrong with it.

I'm trying to test my new storage service by setting, getting, removing and clearing the data I feed it and but have come across this obstacle.

This is my storage.service.spec.ts

import { fakeAsync, TestBed } from '@angular/core/testing';
import { Storage } from '@ionic/storage-angular';
import { of } from 'rxjs';

import { StorageService } from './storage.service';

describe('StorageService', () => {
  let service: StorageService;
  let storageSpy: jasmine.SpyObj<Storage>;

  beforeEach(() => {
    storageSpy = jasmine.createSpyObj('Storage', ['get', 'set', 'remove', 'clear']);
    TestBed.configureTestingModule({
      providers: [
        {
          provide: Storage, useValue: storageSpy
        }
      ]
    });
    service = TestBed.inject(StorageService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should store a value with a key', fakeAsync(() => {
    const key = 'key';
    const value = 'value';

    storageSpy.get.and.returnValue(of(value));
    service.set(key, value);
    const result = service.get(key);

    expect(result).toEqual(value);
    expect(storageSpy.get).toHaveBeenCalled();
  }));
});

This is my storage.service.ts

import { Injectable } from '@angular/core';

import { Storage } from '@ionic/storage-angular';

@Injectable({
  providedIn: 'root'
})
export class StorageService {

  constructor(private storage: Storage) { }

  async get(key: string) {
    return await this.storage.get(key);
  }

  async set(key: string, value: any) {
    return await this.storage?.set(key, value);
  }

  async remove(key: string) {
    await this.storage.remove(key);
  }

  async clear() {
    await this.storage.clear();
  }
}

I don't quite understand the mock up of the values either within storageSpy.get.and.returnValue(of(value));

0 Answers
Related