Skip to content

Using TestBed with NativeScript #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hypery2k opened this issue Sep 29, 2016 · 2 comments
Closed

Using TestBed with NativeScript #478

hypery2k opened this issue Sep 29, 2016 · 2 comments

Comments

@hypery2k
Copy link
Contributor

hypery2k commented Sep 29, 2016

How can I use TestBed with NativeScript. This spec is not working:

import 'reflect-metadata';
import {Router} from '@angular/router';
import {BaseRequestOptions, Response, Http, ResponseOptions} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
import {Page} from 'ui/page';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
import {inject, TestBed} from '@angular/core/testing';
import {SettingsService, AuthService, Logger, User} from '@abc/angular-common';
import * as appSettings from 'application-settings';
import {ApplicationSettingsService} from '../../services/app_settings.service';
import {AppLogService} from '../../services/app_logger.service';
import {RouterMock} from '../mocks/router.mock';
import {ProfileComponent} from '../../components/profile/profile.component';
import {MockConnection} from '@angular/http/testing/mock_backend';

TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());

describe('ProfileComponent', () => {

  // provide our implementations or mocks to the dependency injector
  beforeEach(() => {
    appSettings.clear();
    TestBed.configureTestingModule({
      providers: [
        {provide: Router, useClass: RouterMock},
        {provide: Logger, useClass: AppLogService},
        {provide: SettingsService, useClass: ApplicationSettingsService},
        BaseRequestOptions,
        MockBackend,
        {
          provide: Http,
          useFactory: (backend, defaultOptions) => {
            return new Http(backend, defaultOptions);
          },
          deps: [MockBackend, BaseRequestOptions]
        },
        AuthService,
        Page,
        ProfileComponent
      ]
    });
  });

  it('should init', inject([ProfileComponent], (profile) => {
    expect(profile).toBeDefined();
  }));

  it('should get user on init', inject([ProfileComponent, AuthService], (profile, authService) => {
    let user: User = new User();
    user.username = 'test';

    spyOn(authService, 'getUser').and.callFake(() => {
      return user;
    });

    // trigger init to get user
    profile.ngOnInit();
    expect(authService.getUser).toHaveBeenCalled();
    expect(profile.user).toBe(user);
  }));

});

TestBed would be nice as it gives the ability to have several injects in a test case

@hypery2k
Copy link
Contributor Author

any news on this?

@hdeshev
Copy link
Contributor

hdeshev commented Dec 9, 2016

Closing in favor of #479

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants