Test-Driven Development in ASP.NET Core: My Hands-on Journey with ArcCare using xUnit & Moq
TDD in Action

Test-Driven Development in ASP.NET Core: My Hands-on Journey with ArcCare using xUnit & Moq

Hey everyone!

Welcome to the 4th and final edition of my newsletter, ASP.NET Core Apps. This one is special because it’s not just about TDD in theory; it’s about how I applied it in practice while building ArcCare, my personal project: a hospital management system built with ASP.NET Core.

Why TDD?

When I started developing ArcCare, I knew it was going to grow. It needed to be scalable, reliable, and easy to maintain. That’s when I leaned into test-driven development. TDD isn’t just a developer’s buzzword — it’s a mindset. It forces you to think like the system, write only what’s needed, and build confidence in your codebase.

My Tech Stack for Testing:

  • xUnit: For writing unit tests — clean, fast, and flexible.
  • Moq: For mocking dependencies — essential for testing services and repositories without relying on the database.
  • ASP.NET Core with Repository-Service Pattern (the architecture ArcCare is built on).


Here’s What I Did…!

1. Start With the Test

Before writing a single line of implementation logic, I wrote my test case first. For example, I wanted to test if the PatientService returns the correct list of patients, so I defined the expected behavior before building the method.

2. Mock the Dependencies

Using Moq, I mocked repositories and DB contexts so that my tests stayed fast and isolated. This helped me test business logic without touching the database.

var mockRepo = new Mock<IPatientRepository>();
mockRepo.Setup(repo => repo.GetAll()).ReturnsAsync(GetFakePatients());
        

3. Red – Green – Refactor

If the test failed initially (red), that was a good sign — it meant my test was working! I then wrote the code to pass the test (Green) and finally cleaned it up for readability and reuse (Refactor).

4. Service Layer Testing

Most of the testing revolved around services like

  • PatientService
  • AppointmentService
  • DoctorService

These services are where most of the logic lives in ArcCare, and they were ideal for unit testing.

5. Edge Cases & Negative Scenarios

I didn’t stop at the “happy path.” I wrote tests for:

  • Null inputs
  • Invalid patient IDs
  • Empty database responses

That’s where TDD truly shines — thinking ahead and handling what might break.


What I Learned

  1. TDD builds confidence: Every time I deployed a new feature in ArcCare, I was confident that nothing else would break.
  2. It makes you intentional: you only write the code that solves a clearly defined problem.
  3. Mocking saves time: Moq helped me write dozens of tests without touching real DB calls — it made everything snappier.
  4. Tests evolve with your app. As ArcCare grew, so did my test suite. And that growth was never a burden — it was a support system.


Testing the PatientService

[Fact] 
public async Task GetAllPatients_ReturnsAllPatients()
 { 
    // Arrange 
var mockRepo = new Mock<IPatientRepository>(); 
mockRepo.Setup(repo => repo.GetAll())
.ReturnsAsync(GetFakePatients()); 
var service = new PatientService(mockRepo.Object); 
   // Act 
var result = await service.GetAllPatients();
    // Assert 
Assert.Equal(2, result.Count());
 }        

This simple test gave me so much peace of mind when refactoring the service.


Final Thoughts

Test-Driven Development changed the way I think and build. It slowed me down at the start but made everything faster and smoother later. ArcCare is a stronger, cleaner, and more reliable system because of TDD.

If you’re building apps in ASP.NET Core and haven't tried TDD yet — I encourage you to take the leap. Start small, be consistent, and you'll see the benefits unfold with every line you test.


Let’s talk! Have you used TDD in your projects? Or are you just starting out? Drop your thoughts, and let’s share our journeys.

Until next time, stay sharp, stay curious.

[Swetha Chandran] ASP.NET Core Full Stack Developer | ArcCare Creator

To view or add a comment, sign in

More articles by Swetha Chandran

Insights from the community

Others also viewed

Explore topics