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

MockService Not Working in Angular 8 #420

Closed
3 tasks
DineshKumarGovindarajan87 opened this issue Jun 28, 2019 · 3 comments
Closed
3 tasks

MockService Not Working in Angular 8 #420

DineshKumarGovindarajan87 opened this issue Jun 28, 2019 · 3 comments

Comments

@DineshKumarGovindarajan87

Description

MockService test case failed.

Issue

Environment

  • node -v output: 10.16.0
  • npm -v (or yarn --version) output: 6+
  • npm ls testdouble (or yarn list testdouble) version: 3.3.0

Failing Test

TypeError: this.appTitle.setTitle is not a function
at new AppComponent (http://localhost:9876/_karma_webpack_/webpack:/src/app/app.component.ts:14:19)
at createClass (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:24580:1)
at createDirectiveInstance (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:24400:1)
at createViewNodes (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:35006:1)
at createRootView (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:34878:1)
at callWithDebugContext (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:36407:1)
at Object.debugCreateRootView [as createRootView] (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:35644:1)
at ComponentFactory_.create (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:23542:1)
at initComponent (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/testing.js:3409:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:359:1)

Example Repo

var td = require('testdouble')

// Your steps here.
``` 1. beforeEach(async(() => {
   mockAppTitle = td.object(Title.prototype);

    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
      providers: [
       { provide: Title , useValue: mockAppTitle}
      ]
    }).compileComponents();
  }));
  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    compiled = fixture.debugElement.nativeElement;
  });
  it('should set tilte in header', () => {
    expect(mockAppTitle.setTitle('Demo'));
  });
});

2. In Constructor I assigned the titleService  and used setTitle('Demo') method. 

How to use mockservice in angular 8. any new syntax updated in testdouble 
![Capture](https://user-images.githubusercontent.com/46916258/60346741-6d2b0680-99d9-11e9-9c17-9e11dffbaa0c.PNG)
![Screen1](https://user-images.githubusercontent.com/46916258/60346743-6d2b0680-99d9-11e9-90af-26c57ce4e903.PNG)
![Screen2](https://user-images.githubusercontent.com/46916258/60346744-6dc39d00-99d9-11e9-993a-afe993283419.PNG)


?
Please revert me any suggestion.
@DineshKumarGovindarajan87
Copy link
Author

Screen1
Screen2
Capture3

@tgroshon
Copy link

Thanks for taking the time to create an issue and including sample code. We have not had a chance to look into the details of your particular case but should by early next week (perhaps later with the upcoming US Holiday).

Standby!
:)

@rosston
Copy link
Member

rosston commented Jul 1, 2019

@DineshKumarGovindarajan87 From the information you have provided, it sounds like you are running into an issue with using Angular's dependency injection system. You might want to investigate more on the Angular side of things to determine how to provide a mocked Title.

Additionally, you might want to look at the documentation for td.verify once you've figured out how to provide the mocked value.

I think we should close this issue for now since I think it is outside of the scope of the testdouble.js project.

@rosston rosston closed this as completed Jul 1, 2019
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

No branches or pull requests

3 participants