Mock RepositoryService Test
See original GitHub issueHello,
Is it possible to mock my services with RepositoryService extended ? Exemple I have a service and I want to test my customMethod
user.service.ts
@Injectable()
export class UsersService extends RepositoryService<User> {
constructor(
@InjectRepository(User)
private readonly usersRepository: Repository<User>,
) {
super(usersRepository);
}
public findByCognitoId(cognitoId: string): Observable<User> {
return from(
this.usersRepository.findOneOrFail({ cognitoId: cognitoId }),
);
}
public customMethod() {
return null;
}
}
user.service.spec.ts:
describe('UsersService', () => {
let service: UsersService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UsersService,
{ provide: getRepositoryToken(User), useClass: Repository },
],
}).compile();
service = module.get<UsersService>(UsersService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
But I get an error : TypeError: Cannot read property 'columns' of undefined
thanks
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Unit Tests How to Mock Repository Using Mockito
This way you can control and test all the other parts of your classes(both service and dataService) and mock only repository calls.
Read more >How to mock repository findById thenReturn() Optional?
Try to mock a repository findById method, but no idea use thenReturn() to return an object, as it accepts an Optional ? P.S...
Read more >Mock a Database Repository using Moq - CodeProject
The test class that follows demonstrates how to use Moq to set up a mock Products repository based on the interface above. The...
Read more >How to test services, endpoints, and repositories in Spring Boot
Why is it necessary to write unit test requires another article to explain ... We mock the repository and inject our mocks into...
Read more >org.camunda.bpm.engine.test.mock.Mocks Java Examples
This page shows Java code examples of org.camunda.bpm.engine.test.mock.Mocks. ... endEvent() .done(); deploymentId = repositoryService.createDeployment() .
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here is what I came up with:
This is far from perfect and I can’t imagine keeping this kind of mock if I want to perform more complex tests. I think it is worth the pain to also ask this question to the TypeORM guys.
thanks it’s works now with metadata field 😃