Skip to content

Add getInstanceWithProviders() utility for testing services with dependencies #528

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test: add unit tests for getInstanceWithProviders function
  • Loading branch information
Paratron committed Jun 2, 2025
commit a406ee4f0bbe0b9d738c694630b81f179e2e117c
28 changes: 28 additions & 0 deletions projects/testing-library/tests/getInstanceWithProviders.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import { getInstanceWithProviders } from '../src/lib/testing-library';

describe('getInstanceWithProviders', () => {
@Injectable()
class DatabaseService {
getData() {
return 'real data';
}
}

@Injectable()
class UserService {
constructor(private db: DatabaseService) {}

getUser() {
return `User: ${this.db.getData()}`;
}
}

it('should inject a mock service into a service that depends on it', () => {
const mockDatabase = { getData: () => 'mock data' };

const userService = getInstanceWithProviders(UserService, [{ provide: DatabaseService, useValue: mockDatabase }]);

expect(userService.getUser()).toBe('User: mock data');
});
});