So I have 2 classes. ItemWithAttributes
and ItemWithNameAndAttributes
(derives from 1st).
using System;
using System.Collections.Generic;
using System.Linq;
using Helpers;
namespace Objects.Base
{
public class ItemWithAttributes
{
public IEnumerable<string> Attributes { get; }
public ItemWithAttributes(IEnumerable<string> attributes)
{
if (attributes == null)
throw new ArgumentNullException(nameof(attributes), "Parameter can not be mull.");
Attributes = attributes.ToArray();
}
public virtual string AttributesToParenthesesString(bool prependSpace)
{
return $"{Attributes.ToParenthesesString(prependSpace)}";
}
public override string ToString()
{
return AttributesToParenthesesString(false);
}
}
}
using System;
using System.Collections.Generic;
namespace Objects.Base
{
public class ItemWithNameAndAttributes : ItemWithAttributes
{
public string Name { get; }
public ItemWithNameAndAttributes(string name, IEnumerable<string> attributes) : base(attributes)
{
if (name == null)
throw new ArgumentNullException(nameof(name), "Parameter can not be null.");
if (name.Length == 0)
throw new ArgumentException("Parameter can not be empty.", nameof(name));
Name = name;
}
public override string ToString()
{
return $"{Name}{AttributesToParenthesesString(true)}";
}
}
}
I want to get into unit testing so this is what I got so far for both my classes. I did some reading on naming convention for test method naming but I feel kind of stuck now.
using System;
using System.Collections.Generic;
using System.Linq;
using Objects.Base;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.Objects.Base
{
[TestClass]
public class ItemWithAttributesTests
{
[TestMethod]
public void Constructor_AttributesWithElements_PropertyEqualToInput()
{
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
var itemWithAttributes = new ItemWithAttributes(attributes);
CollectionAssert.AreEqual(attributes, itemWithAttributes.Attributes.ToList());
}
[TestMethod]
public void Constructor_AttributesWithoutElements_PropertyEqualToInput()
{
var attributes = new List<string>() { };
var itemWithAttributes = new ItemWithAttributes(attributes);
CollectionAssert.AreEqual(attributes, itemWithAttributes.Attributes.ToList());
}
[TestMethod]
public void Constructor_AttributesNull_ThrowArgumentNullException()
{
Assert.ThrowsException<ArgumentNullException>(() => new ItemWithAttributes(null));
}
[TestMethod]
public void AttributesToParenthesesString_AttributesWithElementsAndPrependFalse_ReturnsEqualString()
{
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
var itemWithAttributes = new ItemWithAttributes(attributes);
var result = itemWithAttributes.AttributesToParenthesesString(false);
Assert.AreEqual("(Item 1) (Item 2) (Item 3) (Item 4) (Item 5)", result);
}
[TestMethod]
public void AttributesToParenthesesString_AttributesWithElementsAndPrependTrue_ReturnsEqualString()
{
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
var itemWithAttributes = new ItemWithAttributes(attributes);
var result = itemWithAttributes.AttributesToParenthesesString(true);
Assert.AreEqual(" (Item 1) (Item 2) (Item 3) (Item 4) (Item 5)", result);
}
[TestMethod]
public void AttributesToParenthesesString_AttributesWithoutElementsAndPrependFalse_ReturnsEqualString()
{
var attributes = new List<string>() { };
var itemWithAttributes = new ItemWithAttributes(attributes);
var result = itemWithAttributes.AttributesToParenthesesString(false);
Assert.AreEqual("", result);
}
[TestMethod]
public void AttributesToParenthesesString_AttributesWithoutElementsAndPrependTrue_ReturnsEqualString()
{
var attributes = new List<string>() { };
var itemWithAttributes = new ItemWithAttributes(attributes);
var result = itemWithAttributes.AttributesToParenthesesString(true);
Assert.AreEqual("", result);
}
[TestMethod]
public void ToString_AttributesWithElements_ReturnsEqualString()
{
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
var itemWithAttributes = new ItemWithAttributes(attributes);
var result = itemWithAttributes.ToString();
Assert.AreEqual("(Item 1) (Item 2) (Item 3) (Item 4) (Item 5)", result);
}
[TestMethod]
public void ToString_AttributesWithoutElements_ReturnsEqualString()
{
var attributes = new List<string>() { };
var itemWithAttributes = new ItemWithAttributes(attributes);
var result = itemWithAttributes.ToString();
Assert.AreEqual("", result);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Objects.Base;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.Objects.Base
{
[TestClass]
public class ItemWithNameAndAttributesTests
{
[TestMethod]
public void Constructor_NameAndAttributesWithElements_PropertiesAreEqualToInput()
{
var name = "Name";
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
var itemWithNameAndAttributes = new ItemWithNameAndAttributes(name, attributes);
Assert.AreEqual(name, itemWithNameAndAttributes.Name);
CollectionAssert.AreEqual(attributes, itemWithNameAndAttributes.Attributes.ToList());
}
[TestMethod]
public void Constructor_NameAndAttributesWithoutElements_PropertiesAreEqualToInput()
{
var name = "Name";
var attributes = new List<string>() { };
var itemWithNameAndAttributes = new ItemWithNameAndAttributes(name, attributes);
Assert.AreEqual(name, itemWithNameAndAttributes.Name);
CollectionAssert.AreEqual(attributes, itemWithNameAndAttributes.Attributes.ToList());
}
[TestMethod]
public void Constructor_NameEmpty_ThrowArgumentException()
{
var name = string.Empty;
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
Assert.ThrowsException<ArgumentException>(() => new ItemWithNameAndAttributes(name, attributes));
}
[TestMethod]
public void Constructor_NameNull_ThrowArgumentNullException()
{
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
Assert.ThrowsException<ArgumentNullException>(() => new ItemWithNameAndAttributes(null, attributes));
}
[TestMethod]
public void Constructor_AttributesNull_ThrowArgumentNullException()
{
var name = "Name";
Assert.ThrowsException<ArgumentNullException>(() => new ItemWithNameAndAttributes(name, null));
}
[TestMethod]
public void AttributesToParenthesesString_AttributesWithElementsAndPrependFalse_ReturnsEqualString()
{
var name = "Name";
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
var itemWithNameAndAttributes = new ItemWithNameAndAttributes(name, attributes);
var result = itemWithNameAndAttributes.AttributesToParenthesesString(false);
Assert.AreEqual("(Item 1) (Item 2) (Item 3) (Item 4) (Item 5)", result);
}
[TestMethod]
public void AttributesToParenthesesString_AttributesWithElementsAndPrependTrue_ReturnsEqualString()
{
var name = "Name";
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
var itemWithNameAndAttributes = new ItemWithNameAndAttributes(name, attributes);
var result = itemWithNameAndAttributes.AttributesToParenthesesString(true);
Assert.AreEqual(" (Item 1) (Item 2) (Item 3) (Item 4) (Item 5)", result);
}
[TestMethod]
public void AttributesToParenthesesString_AttributesWithoutElementsAndPrependFalse_ReturnsEqualString()
{
var name = "Name";
var attributes = new List<string>() { };
var itemWithNameAndAttributes = new ItemWithNameAndAttributes(name, attributes);
var result = itemWithNameAndAttributes.AttributesToParenthesesString(false);
Assert.AreEqual("", result);
}
[TestMethod]
public void AttributesToParenthesesString_AttributesWithoutElementsAndPrependTrue_ReturnsEqualString()
{
var name = "Name";
var attributes = new List<string>() { };
var itemWithNameAndAttributes = new ItemWithNameAndAttributes(name, attributes);
var result = itemWithNameAndAttributes.AttributesToParenthesesString(true);
Assert.AreEqual("", result);
}
[TestMethod]
public void ToString_AttributesArgumentIsListWithElements_ReturnsEqualString()
{
var name = "Name";
var attributes = new List<string>() { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
var itemWithNameAndAttributes = new ItemWithNameAndAttributes(name, attributes);
var result = itemWithNameAndAttributes.ToString();
Assert.AreEqual("Name (Item 1) (Item 2) (Item 3) (Item 4) (Item 5)", result);
}
[TestMethod]
public void ToString_AttributesArgumentIsListWithoutElements_ReturnsEqualString()
{
var name = "Name";
var attributes = new List<string>() { };
var itemWithNameAndAttributes = new ItemWithNameAndAttributes(name, attributes);
var result = itemWithNameAndAttributes.ToString();
Assert.AreEqual("Name", result);
}
}
}
What I notice is that the unit tests for my derived class ItemWithNameAndAttributes
are almost the same as for my base class ItemWithAttributes
but just a little more and different because I also need to validate for the name argument. I'm not sure if this is the correct way to go or if there is some method to reuse the tests from my base class in my derived class. If I keep this current pattern up, when I derive from ItemWithNameAndAttributes
I would have even more tests that are the same and retest the base classes. I got the feeling it got very complex very quickly for the small classes I have.