Unit testing a class that wraps Storage

I now want to start trying to add some unit tests (after not being able to get it working some time ago). I’ve downloaded and run the example here, and also followed the documentation here.

Previously, one of the first things I tried to test (before even getting to any UI related test), was a couple of simple utility classes. One was a simple class that wraps the Ionic storage

It is declared as follows…

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

@Injectable()
export class DataStorageService {  

constructor(
    private storage: Storage, 
    private logger : Logger
  ) { 
...

}

My test class is as follows

import { async, TestBed, inject } from '@angular/core/testing';
import { IonicModule, Platform } from 'ionic-angular';
import { IonicStorageModule } from '@ionic/storage';
import { DataStorageService } from './data-storage-service';
import { Logger } from './logger';

class TestData {
  public val1 : string;
  public val2 : string;

   /**
   * Deserialise a json string into the member tof this class
   */
  public deserialise(json: any): void {
    this.val1 = json.val1;
    this.val2 = json.val2;    
  }
}

describe('DataDataStorageService', () => {
    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [],
            providers: [
              DataStorageService,                  
              Logger                        
            ],
 
            imports: [   
              IonicStorageModule.forRoot()                                           
            ]
        }).compileComponents(); 
    }));

    beforeEach(() => {     
    });
 
    afterEach(() => {
     //   fixture.destroy();
    //    dataStore = null;       
    });

    it('should return a value that has been set', inject([Storage, Logger], (storage, logger) => {
      let dataStore = new DataStorageService(storage, logger)
      // Set an object - we'll use TestData
      let data = new TestData();
      data.val1 = "value1";
      data.val2= "value2"
      let key = "key";
      dataStore.set<TestData>(key, data).then(() => {
        // Now get it back
        dataStore.get<TestData>(TestData, key).then(val =>{
           expect(val).toBeDefined();
           expect(val instanceof TestData).toBeTruthy();
           expect(val.val1).toEqual(data.val1);
           expect(val.val2).toEqual(data.val2);          
        });
      });       
    }));
});

I’m not sure about the
imports: [ IonicStorageModule.forRoot() ]

The error starts off with…

webpack: Compiled successfully.
Chrome 60.0.3112 (Windows 10 0.0.0) DataDataStorageService should return a value that has been set FAILED
        Failed: Can't resolve all parameters for DataStorageService: (?, ?).
        Error: Can't resolve all parameters for DataStorageService: (?, ?).
            at syntaxError (webpack:///node_modules/@angular/compiler/@angular/compiler.es5.js:1540:21 <- test-config/karma-test-shim.js:41964:

Some time ago, when I first tried this, I found to inject dependencies into a constructor, to use

inject([Storage, Logger], (storage, logger) => {… as above.

Perhaps this isn’t correct any more?

Any help here greatly appreciated!

I think you would be happier if you mock Storage and Logger instead of trying to use the real services.

@rapropos, actually that is a good point.