Unit testing using two functions

Viewed 352

I've neglected unit testing for some time. I wrote unit tests, but they were fairly poor. I'm now reading through "The art of unit Testing" to bring myself up to scratch.

If I have an interface such as:

public interface INotificationService
{
    void AddError(string _error);
    void AddIssue(string _issue);

    IEnumerable<string> FetchErrors();
    IEnumerable<string> FetchIssues();
}

A concrete implementation of this interface contains:

    private readonly ICollection<Message> messages;

Adding an error or issue creates a new message with an enum denoting it's type and adds it to the collection. Calling FetchErrors() / FetchIssues() returns messages of that type from the collection.

Would the following test be valid?:

    [Test]
    public void FetchErrors_LoggingEnabledAddErrorFetchErrors_ReturnsError()
    {
        notificationService = new NotificationService();
        notificationService.AddError("A new error");

        Assert.AreEqual(new []{"A new error"}, notificationService.FetchErrors());
    }

My concern is that I'm first calling AddError(), then testing the result of FetchErrors(). So I'm calling two functions. Is this incorrect?

Should I make the collection public and directly assert that it contains a message of the appropriate type containing the logged error message?

What would be the best practice in this scenario?

4 Answers
Related