cody-greene / node-rabbitmq-client

RabbitMQ (0-9-1) client library with auto-reconnect & zero dependencies
MIT License
116 stars 8 forks source link

How to mock connection on tests? #22

Closed leopucci closed 1 year ago

leopucci commented 1 year ago

Hey there! Nice job man! I am trying to use but i am having a problem with mocking up the conection while testing or generating documentation. While I generate the documentation the connection is made.. I am trying to find a way to mock up the conections during tests. I already have a variable that tells me if it is testing or not, i just need the mockup with the same functions. Anybody already created something like?

Thanks bro!

cody-greene commented 1 year ago

I suggest you avoid mocking the connection but if you really want to then there are libraries for this. Sinon, for example, provides stubs and spies which you could use to create a fake publisher.

-------- Original Message -------- On Jun 23, 2023, 6:15 AM, Leonardo Pucci wrote:

Hey there! Nice job man! I am trying to use but i am having a problem with mocking up the conection while testing or generating documentation. While I generate the documentation the connection is made.. I am trying to find a way to mock up the conections during tests. I already have a variable that tells me if it is testing or not, i just need the mockup with the same functions. Anybody already created something like?

Thanks bro!

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

leopucci commented 1 year ago

Thank you man! Could you share your thoughts about avoiding to mock? how do you test code when you don´t mock? you need to have a server to answer? I will use the sinon for it I think.. thanks!

cody-greene commented 1 year ago

Yes, just run a rabbitmq instance in a docker container or something.

Others have written aritcles more eloquently, discussing the pros and con of mocks. They do have a place in your testing toolkit, but for something like rabbitmq and other "database-like" services I think it's unhelpful, and even harmful. Do you have any SQL in your code? Do you mock the SQL database connection too? How do you know the queries actually work? How much time do you spend maintaining your mock objects?

I have far more confidence in my tests when I'm using a real database instance. And I don't worry about keeping my mock API in sync with the real API either.

leopucci commented 1 year ago

I do agree with you, but the company that i work for uses github actions and ci/cd over github that makes the deploys. they don´t have access to the postgres or mongo servers.. or rabbit.. locally whe can test with the database but on ci/cd we can´t that´s why the tests are always mocked with sinon even the axios requests, etc Thanks man for your thoughts, Have a nice day!

cody-greene commented 1 year ago

https://github.com/cody-greene/node-rabbitmq-client/blob/master/.github/workflows/run-test.yml#L24-L28