UPDATE: I made major changes to this post - check the changelog for details.
I am starting to dive into TDD with NUnit and although I liked checking out some of the resources I found here on stackoverflow, I often don’t find good traction.
What I'm really trying to achieve is to get some kind of checklist / workflow — and here, where I need you guys to help me — or a “Test Plan” that will give me decent code coverage.
So, let's say we have an ideal scenario in which we could start a project from scratch, and let's say the Mailer helper class, which will have the following code:
(I created the class just to help with the code sample issue, so any criticism or advice is encouraged and will be very welcome)
Mailer.cs
using System.Net.Mail; using System; namespace Dotnet.Samples.NUnit { public class Mailer { readonly string from; public string From { get { return from; } } readonly string to; public string To { get { return to; } } readonly string subject; public string Subject { get { return subject; } } readonly string cc; public string Cc { get { return cc; } } readonly string bcc; public string BCc { get { return bcc; } } readonly string body; public string Body { get { return body; } } readonly string smtpHost; public string SmtpHost { get { return smtpHost; } } readonly string attachment; public string Attachment { get { return Attachment; } } public Mailer(string from = null, string to = null, string body = null, string subject = null, string cc = null, string bcc = null, string smtpHost = "localhost", string attachment = null) { this.from = from; this.to = to; this.subject = subject; this.body = body; this.cc = cc; this.bcc = bcc; this.smtpHost = smtpHost; this.attachment = attachment; } public void SendMail() { if (string.IsNullOrEmpty(From)) throw new ArgumentNullException("Sender e-mail address cannot be null or empty.", from); SmtpClient smtp = new SmtpClient(); MailMessage mail = new MailMessage(); smtp.Send(mail); } } }
MailerTests.cs
using System; using NUnit.Framework; using FluentAssertions; namespace Dotnet.Samples.NUnit { [TestFixture] public class MailerTests { [Test, Ignore("No longer needed as the required code to pass has been already implemented.")] public void SendMail_FromArgumentIsNotNullOrEmpty_ReturnsTrue() {
So, after the first two unsuccessful tests, the next obvious step will be to implement the functionality so that they pass, but should I continue the tests with an error and create new ones after implementing the code that will force them to pass, or should I change the existing ones after passing them?
Any advice on this topic would really be greatly appreciated.