❔ C# - How to set up hexagonal architecture in a c# project using TDD
This exercise is a challenge of hexagonal architecture c#, it is implemented by step with a first focus on the business domain in TDD.
Step 1 - The business model
Implementation of the business logic of a bank account:
Expected functionalities:
1. Money deposit
2. Money withdrawal
3. View current balance
4. Consult previous transactions
And in this exercise, I have to propose a suitable object modeling of the entities needed for these functionalities. With unit tests in addition
Currently, I'm new to TDD and I struggle to understand this principe : Test First.
If someone can explain me clearly. I can provide what I did so far.
16 Replies
test first - write a test for some business model first, before creating and writing the (specific parts of the) business model
Oh okay. Because I created the architecture of the code first before. I was thinking doing the tests after. But okay, I do have to delete everything ?
what projects do yo have currently?
if you're doing tdd for the business logic, then the business logic project should start empty, yeah
Possibly with some empty classes defined
Here the structure of my solution .sln :
- API project
- Library classes project :
- Business
- Account.cs
- Transaction.cs
- Data
- Account.cs
- Transaction.cs
-Services
- Repositories
- IAccount.cs (interface)
- AccountService.cs
- Test project (xUnit)
- AccountTest.cs
So far it is what I did. All classes are empty. It allows me to have a first step to write unit test
👍
So I wrote some use case :
Create an Account object with an initial balance.
Call the Withdraw method of the AccountService with a specified amount.
Assert that the method returns the correct balance after the withdrawal.
Assert that the balance of the Account object has been updated correctly.
Why are you bringing in the service already?
Unit tests should rarely have more than one assert. Are you mean to extract finer-grained requirements from your use case?
Oh my bad I copied the wrong text. Here the good one :
Depositing a positive amount should increase the account balance.
Attempting to deposit a zero or negative amount should raise an exception.
Withdrawal of an amount less than the current balance should decrease the account balance.
Attempting to withdraw an amount greater than or equal to the current balance should remove an exception.
yeah, so you can have one test for each of those
Exactly ! So I'm going to do the TDD cycle : Red, Green and Refractor. I'm coming soon with the first test
at the risk of derailing the thread, the hell is hexagonal architecture?
Same thing as onion arch/clean arch/ports+adapters
The term was unknown for me at first but it seems to be a program oriented business logic. I'm still doing it right now
Here the git folder of what I've been doing so far. You will find the subject in a README.md file. : https://github.com/harili/project-training/tree/main/BankAccountProject/BankAccount.Core.Tests
GitHub
project-training/BankAccountProject/BankAccount.Core.Tests at main ...
Contribute to harili/project-training development by creating an account on GitHub.
Now I'm stuck for a review ?
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.