Jasmine Unit Test, override only property in service

Arayik19

this is my component spec file. It uses CalendarService. Inside CalendarService there are weekNames,monthNames,monthLongNames properties(which are just arrays of strings). Now... CalendarService has a lot of methods inside it. I want to be able to override only these 3 properties in my spec file but keep the rest of the methods and use them in the spec file. The reason why I want to override them is because they use another service to be created(e.g. weekNames = [this.translationService.formatMessage(), ... , ... , ...]). Which makes testing very difficult having a component with a service that has a service inside it.

EDIT: or another solution would be to override the translationservice with a mockservice. However translationservice is located inside CalendarService which uses my component. So i dont know how to do that.

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

    let calendarComponent: FCCalendarComponent;

    beforeEach(waitForAsync(() => {
        TestBed.configureTestingModule({
            declarations: [
                FCCalendarComponent,
                CalendarWrapperComponent
            ],
            imports: [
                BrowserModule,
                FormsModule,
                ReactiveFormsModule,
                FCBaseModule,
                HttpClientModule,
                FCTranslateModule,
                FCCalendarModule
            ],
            providers: [
                FCTranslateLoader,
                CalendarService,
                FCTranslationService,
                {
                    provider: CalendarService, useValue: {
                        weekNames: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
                        monthNames: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
                        monthLongNames: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
                    }
                },
            ]
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(CalendarWrapperComponent);
        component = fixture.componentInstance;
        calendarComponent = fixture.debugElement.children[0].componentInstance;
        fixture.detectChanges();
    })


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



@Component({
    selector: 'calendar-wrapper',
    template: `
        <fc-calendar value="2001.02.02" 
            minDate="2000.03.03" 
            maxDate="2002.04.04">
        </fc-calendar>
    `
})
export class CalendarWrapperComponent {

}
Arayik19

SOLUTION: If you have a service in your component and that service has another service used inside it. Instead of mocking the 'parent' service, you can mock the 'child' service. Simply by adding 'child' service to providers like so

FCTranslationService,
{
    provider: FCTranslationService, useValue: { }
},

and in beforEach overriding it with your mock service, like so

TestBed.overrideProvider(FCTranslationService, { useValue: new TranslateMockService() })

and in case you wanna know what mock looks like.

export class TranslateMockService {
    formatMessage() {
        return;
    }
}

The mock only has the method which is being used by the parent service and nothing else.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I mock a service that returns promise in AngularJS Jasmine unit test?

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

mock angularjs service for unit testing using jasmine

From Dev

Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

From Dev

AngularJS controller unit test with Jasmine

From Dev

$httpBackend in AngularJs Jasmine unit test

From Dev

How do I unit test a service-dependent function in Jasmine?

From Dev

Jasmine unit test fails when spying on a service method

From Dev

Jasmine/AngularJS: Inject dependent service to service in unit test?

From Dev

AngularJS Service Jasmine Unit Test Unflushed Request Error

From Dev

Jasmine Unit Test with postMessage and addEventListener

From Dev

Unit testing an angular service method in jasmine

From Dev

Jasmine Unit Test, override only property in service

From Dev

Cannot read property '0' of undefined (only on unit test)

From Dev

Inject real implementation of Angular's $timeout service in a Karma/Jasmine unit test

From Dev

How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

From Dev

Using Jasmine/Karma To Unit Test A Service Won't Inject A Copy Of The Service

From Dev

Jasmine angular unit test 'Cannot read 'property' of undefined

From Dev

Angularjs Unit Test for Service

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

From Dev

TypeError Unit Test Jasmine

From Dev

Jasmine unit test fails when spying on a service method

From Dev

Unit Test for Login with Jasmine and Karma

From Dev

AngularJS Service Jasmine Unit Test Unflushed Request Error

From Dev

Using Jasmine/Karma To Unit Test A Service Won't Inject A Copy Of The Service

From Dev

Angular 2 Observable Service Karma Jasmine Unit Test not working

From Dev

Jasmine unit test module is not defined

From Dev

service undefined jasmine test

Related Related

  1. 1

    How do I mock a service that returns promise in AngularJS Jasmine unit test?

  2. 2

    Unit test service that returns promise Angularjs Jasmine

  3. 3

    mock angularjs service for unit testing using jasmine

  4. 4

    Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

  5. 5

    AngularJS controller unit test with Jasmine

  6. 6

    $httpBackend in AngularJs Jasmine unit test

  7. 7

    How do I unit test a service-dependent function in Jasmine?

  8. 8

    Jasmine unit test fails when spying on a service method

  9. 9

    Jasmine/AngularJS: Inject dependent service to service in unit test?

  10. 10

    AngularJS Service Jasmine Unit Test Unflushed Request Error

  11. 11

    Jasmine Unit Test with postMessage and addEventListener

  12. 12

    Unit testing an angular service method in jasmine

  13. 13

    Jasmine Unit Test, override only property in service

  14. 14

    Cannot read property '0' of undefined (only on unit test)

  15. 15

    Inject real implementation of Angular's $timeout service in a Karma/Jasmine unit test

  16. 16

    How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

  17. 17

    Using Jasmine/Karma To Unit Test A Service Won't Inject A Copy Of The Service

  18. 18

    Jasmine angular unit test 'Cannot read 'property' of undefined

  19. 19

    Angularjs Unit Test for Service

  20. 20

    Unit test service that returns promise Angularjs Jasmine

  21. 21

    Angularjs Unit Test Not Hitting then part of controller after calling service in AngularJS & testing using Karma & Jasmine

  22. 22

    TypeError Unit Test Jasmine

  23. 23

    Jasmine unit test fails when spying on a service method

  24. 24

    Unit Test for Login with Jasmine and Karma

  25. 25

    AngularJS Service Jasmine Unit Test Unflushed Request Error

  26. 26

    Using Jasmine/Karma To Unit Test A Service Won't Inject A Copy Of The Service

  27. 27

    Angular 2 Observable Service Karma Jasmine Unit Test not working

  28. 28

    Jasmine unit test module is not defined

  29. 29

    service undefined jasmine test

HotTag

Archive