How to unit test an Angular service with AngularJS service dependencies
up vote
3
down vote
favorite
I am trying to migrate angular js services to a new angular 7 app. However, each service has many dependencies on other angularjs services and I am not able to convert them all at the same time whilst continuing to create new features. How can I inject angular js services inside unit tests so that I can test my Angular 7 service?
tests.ts // test entry file
import 'core-js/es6';
import 'reflect-metadata';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import getTestBed from '@angular/core/testing';
import
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /.spec.ts$/);
// And load the modules.
context.keys().map(context);
TestService.ts
import UpgradeModule from '@angular/upgrade/static';
import CHECKOUT_SERVICE from '../../../../ajs-upgraded-providers';
@Injectable(
providedIn: 'root'
)
export class TestService
let checkout: any;
constructor(@Inject(CHECKOUT_SERVICE) private checkoutService: any)
this.checkout = checkoutService;
TestService.spec.ts
import TestBed from '@angular/core/testing';
import TestingService from './testing-service';
import CheckoutServiceProvider from 'ajs-upgraded-provider.ts'
describe('testing service with an injected AJS service', () =>
let service: TestingService;
TestBed.configureTestingModule(
providers:[
TestService,
CheckoutServiceProvider
],
imports:[ upgradeModule ]
);
service = TestBed.get(TestingService);
)
it('instance should be created', () =>
expect(expect(service).toBeTruthy();
)
})
When running this unit test I get the following error:
Error: Trying to get the AngularJS injector before it being set.
angularjs angular unit-testing migration angular7
add a comment |
up vote
3
down vote
favorite
I am trying to migrate angular js services to a new angular 7 app. However, each service has many dependencies on other angularjs services and I am not able to convert them all at the same time whilst continuing to create new features. How can I inject angular js services inside unit tests so that I can test my Angular 7 service?
tests.ts // test entry file
import 'core-js/es6';
import 'reflect-metadata';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import getTestBed from '@angular/core/testing';
import
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /.spec.ts$/);
// And load the modules.
context.keys().map(context);
TestService.ts
import UpgradeModule from '@angular/upgrade/static';
import CHECKOUT_SERVICE from '../../../../ajs-upgraded-providers';
@Injectable(
providedIn: 'root'
)
export class TestService
let checkout: any;
constructor(@Inject(CHECKOUT_SERVICE) private checkoutService: any)
this.checkout = checkoutService;
TestService.spec.ts
import TestBed from '@angular/core/testing';
import TestingService from './testing-service';
import CheckoutServiceProvider from 'ajs-upgraded-provider.ts'
describe('testing service with an injected AJS service', () =>
let service: TestingService;
TestBed.configureTestingModule(
providers:[
TestService,
CheckoutServiceProvider
],
imports:[ upgradeModule ]
);
service = TestBed.get(TestingService);
)
it('instance should be created', () =>
expect(expect(service).toBeTruthy();
)
})
When running this unit test I get the following error:
Error: Trying to get the AngularJS injector before it being set.
angularjs angular unit-testing migration angular7
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to migrate angular js services to a new angular 7 app. However, each service has many dependencies on other angularjs services and I am not able to convert them all at the same time whilst continuing to create new features. How can I inject angular js services inside unit tests so that I can test my Angular 7 service?
tests.ts // test entry file
import 'core-js/es6';
import 'reflect-metadata';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import getTestBed from '@angular/core/testing';
import
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /.spec.ts$/);
// And load the modules.
context.keys().map(context);
TestService.ts
import UpgradeModule from '@angular/upgrade/static';
import CHECKOUT_SERVICE from '../../../../ajs-upgraded-providers';
@Injectable(
providedIn: 'root'
)
export class TestService
let checkout: any;
constructor(@Inject(CHECKOUT_SERVICE) private checkoutService: any)
this.checkout = checkoutService;
TestService.spec.ts
import TestBed from '@angular/core/testing';
import TestingService from './testing-service';
import CheckoutServiceProvider from 'ajs-upgraded-provider.ts'
describe('testing service with an injected AJS service', () =>
let service: TestingService;
TestBed.configureTestingModule(
providers:[
TestService,
CheckoutServiceProvider
],
imports:[ upgradeModule ]
);
service = TestBed.get(TestingService);
)
it('instance should be created', () =>
expect(expect(service).toBeTruthy();
)
})
When running this unit test I get the following error:
Error: Trying to get the AngularJS injector before it being set.
angularjs angular unit-testing migration angular7
I am trying to migrate angular js services to a new angular 7 app. However, each service has many dependencies on other angularjs services and I am not able to convert them all at the same time whilst continuing to create new features. How can I inject angular js services inside unit tests so that I can test my Angular 7 service?
tests.ts // test entry file
import 'core-js/es6';
import 'reflect-metadata';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import getTestBed from '@angular/core/testing';
import
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
from '@angular/platform-browser-dynamic/testing';
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /.spec.ts$/);
// And load the modules.
context.keys().map(context);
TestService.ts
import UpgradeModule from '@angular/upgrade/static';
import CHECKOUT_SERVICE from '../../../../ajs-upgraded-providers';
@Injectable(
providedIn: 'root'
)
export class TestService
let checkout: any;
constructor(@Inject(CHECKOUT_SERVICE) private checkoutService: any)
this.checkout = checkoutService;
TestService.spec.ts
import TestBed from '@angular/core/testing';
import TestingService from './testing-service';
import CheckoutServiceProvider from 'ajs-upgraded-provider.ts'
describe('testing service with an injected AJS service', () =>
let service: TestingService;
TestBed.configureTestingModule(
providers:[
TestService,
CheckoutServiceProvider
],
imports:[ upgradeModule ]
);
service = TestBed.get(TestingService);
)
it('instance should be created', () =>
expect(expect(service).toBeTruthy();
)
})
When running this unit test I get the following error:
Error: Trying to get the AngularJS injector before it being set.
angularjs angular unit-testing migration angular7
angularjs angular unit-testing migration angular7
edited Nov 9 at 15:35
Goncalo Peres
8171311
8171311
asked Nov 8 at 14:25
Bunne GilShaw
162
162
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209735%2fhow-to-unit-test-an-angular-service-with-angularjs-service-dependencies%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown