Skip to content
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

feat: Mocking service #88

Merged
merged 2 commits into from
Mar 21, 2020
Merged

feat: Mocking service #88

merged 2 commits into from
Mar 21, 2020

Conversation

satanTime
Copy link
Member

@satanTime satanTime commented Feb 26, 2020

The goal is not to provide an empty object as a provider, but to try to simulate existing methods.

What I tried to do was is to clone https://github.com/gothinkster/angular-realworld-example-app and to add a simple test which checks that app.component is rendered. And found that it fails with the error TypeError: this.userService.populate is not a function and I think if we mock at least methods of providers we should solve almost majority of issues except chaining this.userService.populate().test() but that's impossible without a metadata parser.

import {TestBed} from '@angular/core/testing';
import {BrowserModule} from '@angular/platform-browser';
import {AppRoutingModule} from 'app/app-routing.module';
import {AppComponent} from 'app/app.component';
import {AuthModule} from 'app/auth/auth.module';
import {CoreModule} from 'app/core';
import {HomeModule} from 'app/home/home.module';
import {SharedModule} from 'app/shared';
import {FooterComponent, HeaderComponent} from 'app/shared/layout';
import {MockComponents, MockModule, MockRender} from 'ng-mocks';

describe('test', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        MockModule(BrowserModule),
        MockModule(CoreModule),
        MockModule(SharedModule),
        MockModule(HomeModule),
        MockModule(AuthModule),
        MockModule(AppRoutingModule),
      ],
      declarations: [
        ...MockComponents(FooterComponent, HeaderComponent),
        AppComponent,
      ],
    });
  });

  it('it', () => {
    const fixture = MockRender('<app-root></app-root>');
    expect(fixture.debugElement).toBeDefined();
  });
});

@@ -54,10 +54,31 @@ const mockProvider = (provider: any): Provider | undefined => {
return undefined;
}

// mocking all methods / properties of a class / object.
let value: any;
if (typeof provide === 'function') {
Copy link
Member Author

@satanTime satanTime Feb 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the next PR with the option to set mocking provider from outside I'll move this part to MockService.

@satanTime
Copy link
Member Author

Wait :) need a proper implementation

@satanTime satanTime force-pushed the issues/72 branch 4 times, most recently from 91eb0b8 to cdfa00a Compare February 27, 2020 22:53
@satanTime
Copy link
Member Author

Done

@ike18t ike18t merged commit 7004728 into help-me-mom:master Mar 21, 2020
@satanTime satanTime deleted the issues/72 branch March 21, 2020 08:59
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 this pull request may close these issues.

Cannot mock child component's DI services
2 participants