1

I am using a function in my actual service but I want to know how can i inject that function in unit test case. Below isSameDay is a function not provider. How can i inject it into test case as this function is working fine in actual service.

import { LoggerService, isSameDay } from '../../../shared';

describe('NavBaseComponent', () => {
  let component: NavBaseComponent;
  let fixture: ComponentFixture<NavBaseComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [NavBaseComponent],
      providers: [
        LoggerService
      ]
    })
      .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(NavBaseComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should be created', () => {
    expect(component).toBeTruthy();
  });
});

Code for isSameDay function

export function isSameDay(d1: Date, d2: Date): boolean {
    return !!d1 && !!d2 &&
        d1.getDate() === d2.getDate() && 
        d1.getMonth() === d2.getMonth() &&
        d1.getFullYear() === d2.getFullYear();
};
2
  • Can you add the shared code? Is it a method inside provider or something else? Commented Nov 16, 2017 at 11:21
  • It's a function which is used in many places Commented Nov 16, 2017 at 12:15

3 Answers 3

4
+50

You don't need to inject it anywhere, you can test it as is.

import { LoggerService, isSameDay } from '../../../shared';

describe..

  it('should test isSameDay', ()=>{
      expect(isSameDay(new Date(), new Date())).toEqual(true);
  })

..

Here's a plunker example: http://plnkr.co/edit/TyTyaJvDD28AqPH2yDKa?p=preview

Sign up to request clarification or add additional context in comments.

4 Comments

I don't want to test isSameDay. It's is used inside map(date=> isSameDay(date, new Date())). It is creating issue while test case is running error : isSameDay is not a function. Test case is not aware of this function. How can i fix it?
@Rohit I've changed the plunker, can you check if it reproduces your issue?
I got the issue. Mock service was not returning date for the map(date=> isSameDay(date, new Date())). It was returning string which was creating issue. Thanks for your time.
@Rohit ah, glad you figured it out
0

If I were you, I would use a stub service like providers: [ {provide: UserService, useValue: userServiceStub } ]

and you can get it like this userService = fixture.debugElement.injector.get(UserService); or userService = TestBed.get(UserService);

For example:

TestBed.configureTestingModule({
     declarations: [ WelcomeComponent ],
     providers:    [ {provide: UserService, useValue: userServiceStub } ]
  });

  fixture = TestBed.createComponent(WelcomeComponent);
  comp    = fixture.componentInstance;

  // UserService from the root injector
  userService = TestBed.get(UserService);
  ...

Comments

0

If you need to test isSameDay function, you could convert it into a service and treat it the same as any other service. Just like LoggerService in your case.

The benefit with this approach is that you can test your function/service in isolation.

When writing component tests, you can always mock this new service call. Again,this helps you test your component in isolation.

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.