3

Little exhausted here, may be that is why my title is not so accurate.

I am writing a unit test for my DummyService:

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

    @Injectable()
    export class DummyService {

        getAllDataSources():Promise<Array<DummyData>> {
            return new Promise<DummyData[]>(resolve =>
                setTimeout(()=>resolve([]), 1000) // 1 seconds
            );
        }
    }

Please assume am returning a list of DummyData objects from getAllDataSources.

Now, I have a structure/interface for the dummy data in the same service file:

 export interface DummyData{
            Name:string;
            IsActive:boolean;
        }

I tried to write unit test for this service:

import {DummyService, DummyData} from './dummy.service';
import {
    beforeEachProviders
} from '@angular/core/testing';

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

export function main() {

    describe('dummy.service', () => {
        let dsService:DummyService;

        it('should fetch data', ()=> {
           dummyData: DummyData = new DummyData(); // >>>> culprit...
           expect(1).toEqual(1);
        });
    });
}

This unit test seem little funny, as I am really not calling DummyServices function to get the list of DummyData.

I am doing this because I was getting some issue, due to which I was not able to see my test. I did some research, spent a whole day and finally found that this structure DummyData is the CULPRIT. I proved this to myself when I tried creating an object of it in my unit test (in the code above) and I got the following error:

FAILED TESTS:
  dummy.service
    ✖ should fetch data
      PhantomJS 2.1.1 (Linux 0.0.0)
      Chrome 50.0.2661 (Linux 0.0.0)
    ReferenceError: **DummyData is not defined**
        at eval (/home/aodev/WebstormProjects/Data Federation App/data-mapping-app/dist/dev/app/shared/datasource.service.spec.js:8:28)
        at Object.eval (/home/aodev/WebstormProjects/Data Federation App/data-mapping-app/node_modules/@angular/core/testing/testing.js:80:25)

So, can someone tell me please, what am I doing wrong? Why I cannot create the object of DummyData inside my unit test?

Please help!

2
  • Maybe a missing import for DummyData in your DummyService file. Commented Jun 20, 2016 at 12:04
  • From the code above, it looks like DummyData is an interface and not a class. Interfaces cannot be instantiated. Because they are not classes. Commented Jun 20, 2016 at 12:42

1 Answer 1

2

TypeScript interfaces exist only during compile time, runtime knows nothing about interfaces.

This is how you create instance that implements interface:

interface itest
{
    success:boolean;
}

let a:itest = {success: true}; //compiler checks that object matches interface itest
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.