Похожие чаты

Interfaces are useful in writing code that's easy to test.



For example, You have a function called Router that takes a DB struct.

The DB struct has a field called mdb which is a connection to monogdb database.

A function called InitDB opens a mongdb connection and returns *DB. Then, You can use this value returned by InitDB to call Router.

All looks good, Right??

well, Now, What if you want to write tests for your code?

If you continue with current approach, It means, You'll need a environment where you can run the database, Seed some initial values in database and maybe even reset them after every test.

This quickly becomes a PITA.


What if Router took a interface called IDB. This interface has definitions of all the methods that you'll use in rest of your code.


When you want to run you code in production, The DB struct implements IDB Interface and DB struct can be passed to Router because router takes IDB.

When you want to test your code, You create another struct called FDB that also implements IDB Interface, But instead of establishing connection with a database and everything, It only mocks the database.

For example,
When you store a json blob in database, The database responds with { result: "ok" }, In the method defined on DB struct, it goes ahead and stores the value in database and sends whatever response database sent back.

But in the method defined on FDB, You don't call the database routines at all, You just mock the database response and send it back.

2 ответов

15 просмотров

Thanks, this clears things a bit ^_^

This was a great explanation of interfaces and an example of how to implement them. It should help me with the issue I posted above, thanks for this. At least it gives me a direction to go in.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта