I'm just getting into testing and wanted to see if someone can tell me if I'm writing tests correctly. I'm using C#, NUnit, & Should.
This is the class I'm testing:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Emit;
Using VM.Models.Common;
namespace Vm.Models.CustomerNS
{
public class Customer
{
// ReSharper disable once InconsistentNaming
public int ID { get; set; }
public List<Address> Locations { get; set; }
public List<User> Users { get; set; }
public List<BuyerNum> BuyerNumbers { get; set; }
public Dictionary<string, string> VehicleList { get; set; }
public List<Contact> Contacts { get; set; }
public string Name { get; set; }
public Customer()
{
Locations = new List<Address>();
Users = new List<User>();
BuyerNumbers = new List<BuyerNum>();
Contacts = new List<Contact>();
VehicleList = new Dictionary<string,string>();
}
public void AddLocation(AddressType addtype, string addressName, string streetAddress, string city, string state, string zip, string country)
{
var addressId = 0;
var lastOrDefault = Locations.LastOrDefault();
if (lastOrDefault != null) addressId = lastOrDefault.AddressId + 1;
Address newAddress = new Address
{
AddressId = addressId,
AddressName = addressName,
AddressType = addtype,
City = city,
State = state
};
if (streetAddress != null) newAddress.StreetAddress = streetAddress;
if (zip != null) newAddress.Zip = zip;
Locations.Add(newAddress);
}
public void RemoveLocation(int addressId)
{
Debug.Assert(Locations != null, "Locations != null");
var addressid = Locations.Where(x => x.AddressId == addressId);
var enumerable = addressid as IList<Address> ?? addressid.ToList();
if (enumerable.Count() != 1) { throw new InvalidOperationException("Cannot Delete"); }
Locations.Remove(enumerable.First());
}
public void ModifyLocation(int addressId, AddressType addtype, string addressName, string streetAddress, string city, string state, string zip, string country)
{
var addressid = Locations.Where(x => x.AddressId == addressId);
var enumerable = addressid as Address[] ?? addressid.ToArray();
if (enumerable.Count() != 1) { throw new InvalidOperationException("Cannot Delete"); }
Locations.Remove(enumerable.First());
}
public Address GetLocation(int addressId)
{
return Locations.First(x => x.AddressId == addressId);
}
public List<Address> GetAllLocations()
{
return Locations;
}
}
}
Testing Class
using System.Linq;
using Vm.Models.Common;
using VM.Models.CustomerNS;
using NUnit.Framework;
using Should;
// ReSharper disable once CheckNamespace
namespace vm.Models.CustomerNS.Tests
{
[TestFixture()]
public class CustomerTests
{
[Test()]
public void CustomerTest()
{
var customerObject = new Customer() { Name = "Hello World!!" };
customerObject.ShouldNotBeNull();
Assert.AreEqual(customerObject.Name, "Hello World!!");
}
[Test()]
public void AddLocationTest()
{
var customerObject = new Customer() { Name = "Hello World!!" };
customerObject.AddLocation(AddressType.Business, "Hello Location", "123 Street Name", "Hello City", "HS", "10001", "US");
customerObject.Locations.First().ShouldNotBeNull();
Assert.AreEqual(customerObject.Locations.First().City, "Hello City");
}
[Test()]
public void RemoveLocationTest()
{
var customerObject = new Customer() { Name = "Hello World!!" };
customerObject = GenerateMultipleLocations(5, customerObject);
var inintialCount = customerObject.Locations.Count;
var custObjCopy = customerObject.Locations.First();
var thirdlocation = customerObject.Locations[3];
customerObject.RemoveLocation(1);
customerObject.Locations.Count.ShouldEqual(inintialCount - 1);
customerObject.Locations.First().ShouldEqual(custObjCopy);
customerObject.Locations[3].ShouldNotEqual(thirdlocation);
}
private Customer GenerateMultipleLocations(int p, Customer customer)
{
int i = 0;
while (i < p)
{
customer.AddLocation(AddressType.Business, EfficientlyLazy.IdentityGenerator.Generator.GenerateName().First, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().AddressLine, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().City, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().StateAbbreviation, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().ZipCode, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().City);
i++;
}
return customer;
}
[Test()]
public void ModifyLocationTest()
{
var customerObject = new Customer() { Name = "Hello World!!" };
customerObject = GenerateMultipleLocations(5, customerObject);
var inintialCount = customerObject.Locations.Count;
var custObjCopy = customerObject.Locations.First();
var thirdlocation = customerObject.Locations[3];
customerObject.Locations[3].AddressId.ShouldEqual(3);
customerObject.ModifyLocation(3, AddressType.Business, EfficientlyLazy.IdentityGenerator.Generator.GenerateName().First, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().AddressLine, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().City, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().StateAbbreviation, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().ZipCode, EfficientlyLazy.IdentityGenerator.Generator.GenerateAddress().City);
thirdlocation.ShouldNotEqual(customerObject.Locations[3]);
}
[Test()]
public void GetLocationTest()
{
Assert.Fail();
}
[Test()]
public void GetAllLocationsTest()
{
Assert.Fail();
}
}
}
PS: I realize I haven't tested the bottom 2 methods.