Comments (8)
I recommend that you change the service name to "findOne" or change the service "getOne" to obligatorily return an exception if you don't find a cat. Find you use when you want to search for something but don't know where to find it and Get you use it when you know where a resource is and you only need to access it.
I'm gonna disagree with you here. Mainly for the fact that I know where I'm looking for it and what in looking by. We know it's the cat table, and we know the ID we're searching for. Sure we could get pedantic and say that maybe that ID exists, maybe it doesn't, but honestly that feels like arguing nest to argue.
The verbs Find and Get have a specific meaning in programming.
They have specific meaning in english too, but interpretation is subjective. In my mind, I wanted to find the cat based on the ID. If you feel incredibly strongly about it otherwise, feel free to leave a pull request fixing it (in all the places that use this pattern), with a link to some academic or well respected source able find vs get.
Now back to your question: if by "service" your mean a method of the save service, you'd just end up mocking the dependency's method that this method consumes. In general, you shouldn't really ever mock methods of the class you're trying to test, cause some runners, like jest, can run asynchronously and in parallel and end up giving you mixed results, so instead you should mock the lower dependencies and do an integration of the methods. That, or fully separate the tests to that you mock that method in one unit test suite but not the other.
from testing-nestjs.
"lower dependencies", would it be, for example, the database repository?
Yes , exactly (wasn't sure what else to call them). The updateOne
may depend on the findOne
, but the findOne
depends on the this.model.findOne
so the dependency on this.model
is what would need to be mocked.
a bug in this method would influence the "updateCat".
Yep, and that would be a drawback of having these methods depend on each other without good integration tests of the methods
In these cases, could this not be a problem for unit tests that should only test a portion of code and mock external dependencies?
It definitely could be a problem. Which is why there's integration and e2e testing. This repo has some e2e samples. I don't have anything explicitly for integration tests though.
from testing-nestjs.
Yep, the RxJS-Sample/app.service has a service injecting a service and a mock for it.
from testing-nestjs.
Also, technically, anytime you're doing @InjectRepository()
or @InjectModel()
, you're injecting a service. This just happens to be a very specific service.
from testing-nestjs.
Hi!
Thanks for answering.
Your answer answers my question but I was not referring to that.
I'm sorry for not being clear.
See for example this service to "update a cat". Instead of searching the repository directly if this cat exists and updating it, I could use another service called "getOne" to find out if the cat exists before updating it and returning an error if the cat does not exist.
The advantage of using a service instead of searching and updating by connecting to the database directly would be that I would not need to repeat the same error message on different services. For example, a service to "delete a cat".
My question would be how I could mock this service to "get a cat", because at certain times, it may be desirable for this service to return null, for example, to know what my system's behavior will be if the user type an invalid cat ID.
from testing-nestjs.
In general, you shouldn't really ever mock methods of the class you're trying to test, cause some runners, like jest, can run asynchronously and in parallel and end up giving you mixed results, so instead you should mock the lower dependencies and do an integration of the methods.
"lower dependencies", would it be, for example, the database repository?
That, or fully separate the tests to that you mock that method in one unit test suite but not the other.
That is what I am looking for. In the example, I gave that the "updateCat" method depends on the "getOne" service, even if there is test coverage for the "getOne" method, a bug in this method would influence the "updateCat".
In these cases, could this not be a problem for unit tests that should only test a portion of code and mock external dependencies?
from testing-nestjs.
You're awesome.
Could you close this issue?
Thank you.
from testing-nestjs.
Thanks for the great discussion 😄 Hopefully it was helpful and will help others
from testing-nestjs.
Related Issues (20)
- About import {DocumentQuery} from 'mongoose' at mongo-sample
- Suggestion: convert testing-nestjs into monorepo with Lerna HOT 3
- Suggestion: create tests for Prisma (alternative to TypeORM) HOT 7
- [FAILING TEST] Unit testing a NestJS Service that consumes a TypeORM Repository HOT 6
- [NEW TEST] Testing for interceptor failure HOT 3
- [NEW TEST] KnexJs Sample test HOT 1
- Multiple mocked dependencies with the same content HOT 5
- [NEW TEST] Bull HOT 4
- Prisma transactions and rollbacks now available HOT 2
- [NEW TEST] - (Contract Testing) - Incorporate NestJS Consumer Driven Contract testing with nestjs-pact HOT 6
- README refers to non-existing cat.integration.spec.ts HOT 3
- Test against Solidity smart contracts
- [NEW TEST] Tests for mikro-orm
- [NEW TEST] Sequelize transactions HOT 1
- Add code comment for documenting the tests. HOT 1
- Unable to commit the file in file-up-and-down-sample HOT 1
- Can't push to sidebranch for PR - getting permission denied HOT 1
- I can't seem to make the tests fails anywhere so far HOT 3
- npm install doesn't work
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testing-nestjs.