{"id":30893234,"url":"https://github.com/masterfermin02/unit-testing-tips-csharp","last_synced_at":"2026-02-12T07:07:06.685Z","repository":{"id":306678086,"uuid":"1026920822","full_name":"masterfermin02/unit-testing-tips-csharp","owner":"masterfermin02","description":"Unit testing tips by examples in C#","archived":false,"fork":false,"pushed_at":"2025-07-26T23:02:22.000Z","size":51,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-08T20:06:30.170Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/masterfermin02.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-26T22:54:24.000Z","updated_at":"2025-07-26T23:02:25.000Z","dependencies_parsed_at":"2025-07-27T02:45:38.243Z","dependency_job_id":"85745425-36a2-4617-bbf0-a2720ff3ac15","html_url":"https://github.com/masterfermin02/unit-testing-tips-csharp","commit_stats":null,"previous_names":["masterfermin02/unit-testing-tips-csharp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/masterfermin02/unit-testing-tips-csharp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masterfermin02%2Funit-testing-tips-csharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masterfermin02%2Funit-testing-tips-csharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masterfermin02%2Funit-testing-tips-csharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masterfermin02%2Funit-testing-tips-csharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/masterfermin02","download_url":"https://codeload.github.com/masterfermin02/unit-testing-tips-csharp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masterfermin02%2Funit-testing-tips-csharp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29360768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-12T01:03:07.613Z","status":"online","status_checked_at":"2026-02-12T02:00:06.911Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-09-08T20:06:21.864Z","updated_at":"2026-02-12T07:07:06.668Z","avatar_url":"https://github.com/masterfermin02.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Testing tips\n\nIn these times, the benefits of writing unit tests are huge. I think that most of the recently started projects contain any unit tests. In enterprise applications with a lot of business logic, unit tests are the most important tests, because they are fast and can us instantly assure that our implementation is correct. However, I often see a problem with good tests in projects, though these tests' benefits are only huge when you have good unit tests. So in these examples, I will try to share some tips on what to do to write good unit tests.\n\n## Unit Test Doubles and Naming Guidelines in C#\n\n### Test Doubles\n\nTest doubles are fake dependencies used in tests.\n\n![Test doubles](./assets/test-doubles.jpg ':size=800')\n\n---\n\n### Dummy\n\nA dummy is a simple implementation that does nothing and is only used to satisfy interface requirements.\n\n```csharp\npublic class DummyMailer : IMailer\n{\n    public void Send(Message message)\n    {\n        // No operation\n    }\n}\n```\n\n### Fake\n\nA fake is a working but simplified implementation, often using in-memory logic. It's useful for simulating a real component in a controlled way.\n\n```csharp\npublic class InMemoryCustomerRepository : ICustomerRepository\n{\n    private readonly Dictionary\u003cGuid, Customer\u003e _customers = new();\n\n    public void Store(Customer customer)\n    {\n        _customers[customer.Id] = customer;\n    }\n\n    public Customer Get(Guid id)\n    {\n        if (!_customers.TryGetValue(id, out var customer))\n            throw new CustomerNotFoundException();\n\n        return customer;\n    }\n\n    public Customer FindByEmail(string email)\n    {\n        return _customers.Values.FirstOrDefault(c =\u003e c.Email == email)\n               ?? throw new CustomerNotFoundException();\n    }\n}\n\n```\n\n### Stub\n\nA stub returns predefined responses to method calls.\n\n```csharp\npublic class AlwaysUniqueEmailStub : IUniqueEmailSpecification\n{\n    public bool IsUnique(string email) =\u003e true;\n}\n```\n\nAlternatively, using a mocking library like Moq:\n\n```csharp\nvar stub = new Mock\u003cIUniqueEmailSpecification\u003e();\nstub.Setup(x =\u003e x.IsUnique(It.IsAny\u003cstring\u003e())).Returns(true);\n\n```\n\n### Spy\n\nA spy records information about interactions, which can be asserted later.\n\n```csharp\npublic class SpyMailer : IMailer\n{\n    public List\u003cMessage\u003e SentMessages { get; } = new();\n\n    public void Send(Message message)\n    {\n        SentMessages.Add(message);\n    }\n\n    public int SentCount =\u003e SentMessages.Count;\n}\n\n```\n\n### Mock\n\nA mock is used to verify specific interactions, such as method calls and arguments.\n\n```csharp\nvar mockMailer = new Mock\u003cIMailer\u003e();\nvar message = new Message(\"test@example.com\", \"Test\");\n\nmockMailer.Setup(m =\u003e m.Send(It.Is\u003cMessage\u003e(msg =\u003e msg.Equals(message)))).Verifiable();\n\n// System under test uses mockMailer.Object\n\nmockMailer.Verify(m =\u003e m.Send(It.IsAny\u003cMessage\u003e()), Times.Once);\n```\n\nUse stubs for controlling inputs and mocks for verifying outputs.\n\n### Example: Not Recommended (Overuse of Mocks)\n\n```csharp\n[Test]\npublic void Sends_All_Notifications()\n{\n    var message1 = new Message();\n    var message2 = new Message();\n\n    var repo = new Mock\u003cIMessageRepository\u003e();\n    repo.Setup(x =\u003e x.GetAll()).Returns(new[] { message1, message2 });\n\n    var mailer = new Mock\u003cIMailer\u003e();\n\n    var sut = new NotificationService(mailer.Object, repo.Object);\n\n    sut.Send();\n\n    mailer.Verify(x =\u003e x.Send(message1), Times.Once);\n    mailer.Verify(x =\u003e x.Send(message2), Times.Once);\n}\n```\n\n### Example: Recommended (Using Fakes and Spies)\n\n```csharp\n[Test]\npublic void Sends_All_Notifications()\n{\n    var message1 = new Message();\n    var message2 = new Message();\n\n    var repository = new InMemoryMessageRepository();\n    repository.Save(message1);\n    repository.Save(message2);\n\n    var mailer = new SpyMailer();\n\n    var sut = new NotificationService(mailer, repository);\n    sut.Send();\n\n    CollectionAssert.AreEquivalent(new[] { message1, message2 }, mailer.SentMessages);\n}\n```\n\nAdvantages:\n\n- Easier to maintain and refactor\n- More expressive and readable\n- Lower coupling to mocking framework\n- More resilient to method signature changes\n\n### Test Naming Best Practices\n\n### Poor Examples\n\n```csharp\n[Test]\npublic void Test() { }\n\n[Test]\npublic void TestDeactivateSubscription() { }\n\n[Test]\npublic void ItThrowsWhenPasswordTooShort() { }\n```\n\n### Recommended Style\n\nUse descriptive, behavior-focused names. Prefer underscores for readability.\n\n```csharp\n[Test]\npublic void SignIn_WithInvalidCredentials_IsNotPossible() { }\n\n[Test]\npublic void Creating_WithTooShortPassword_IsInvalid() { }\n\n[Test]\npublic void Deactivating_AnActiveSubscription_Succeeds() { }\n\n[Test]\npublic void Deactivating_AnInactiveSubscription_IsInvalid() { }\n```\n\nGuidelines:\n\n- Avoid technical terms in test names\n- Describe expected behavior clearly\n- Use present tense and readable language\n- Make it understandable to a non-developer if possible\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasterfermin02%2Funit-testing-tips-csharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasterfermin02%2Funit-testing-tips-csharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasterfermin02%2Funit-testing-tips-csharp/lists"}