dependency injection - Mocking injected provided service in standalone angular component AND NOT IN ROOT for karma jasmine tests

FYI: I am using angular 19.1.0 with karmaLet's assume we have this service and component NOT P

FYI: I am using angular 19.1.0 with karma

Let's assume we have this service and component

// NOT PROVIDED IN ROOT!
@Injectable()
export class TestService {
  getData(): string {
    return 'this is real data'
  }
}
// Standalone component!
@Component({
  selector: 'app-root',
  templateUrl: './appponent.html',
  styleUrl: './appponent.scss',
  providers: [TestService]
})
export class AppComponent {
  service = inject(TestService)
}

As you noticed, the service is not provided in root but is provided in the component and injected as well. I noticed with this setting that i am not able to mock this service in the karma unit tests. I can see it has to do with the service being provided in the component, but i am not sure how i can fix it. Here is a snippet of the test.

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [AppComponent],
      providers: [{
        provide: TestService,
        useValue: {
          getData: () => ('mocked data'),
        }
      }]
    })pileComponents();
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixtureponentInstance;

    expect(app).toBeTruthy();

    console.log(app.service.getData())
  });
});

I should expect here that app.service.getData() should log out "mocked data", however, i get "this is real data".

FYI: I am using angular 19.1.0 with karma

Let's assume we have this service and component

// NOT PROVIDED IN ROOT!
@Injectable()
export class TestService {
  getData(): string {
    return 'this is real data'
  }
}
// Standalone component!
@Component({
  selector: 'app-root',
  templateUrl: './appponent.html',
  styleUrl: './appponent.scss',
  providers: [TestService]
})
export class AppComponent {
  service = inject(TestService)
}

As you noticed, the service is not provided in root but is provided in the component and injected as well. I noticed with this setting that i am not able to mock this service in the karma unit tests. I can see it has to do with the service being provided in the component, but i am not sure how i can fix it. Here is a snippet of the test.

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [AppComponent],
      providers: [{
        provide: TestService,
        useValue: {
          getData: () => ('mocked data'),
        }
      }]
    })pileComponents();
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixtureponentInstance;

    expect(app).toBeTruthy();

    console.log(app.service.getData())
  });
});

I should expect here that app.service.getData() should log out "mocked data", however, i get "this is real data".

Share edited Mar 7 at 19:57 Naren Murali 60.7k5 gold badges44 silver badges79 bronze badges asked Mar 7 at 18:05 Abdullah El-KindyAbdullah El-Kindy 731 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The component is not having the mocked service( might be because it was not provided in the root of the application ). So try the overrideProvider method to configure the mock service, this method works.

describe('AppComponent', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [AppComponent],
      providers: [
        {
          provide: TestService,
          useValue: {
            getData: () => 'mocked data',
          },
        },
      ],
    })
    .overrideProvider(TestService, {          // <- changed here!
        useValue: {                           // <- changed here!
          getData: () => 'mocked data',       // <- changed here!
        },                                    // <- changed here!
    })                                        // <- changed here!
    pileComponents();
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixtureponentInstance;

    expect(app).toBeTruthy();

    console.log(app.service.getData());
  });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744914142a4600731.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信