Skip to content

Instantly share code, notes, and snippets.

@alibitek
Forked from hagbarddenstore/Example.cs
Created January 12, 2014 10:42
Show Gist options
  • Save alibitek/8383083 to your computer and use it in GitHub Desktop.
Save alibitek/8383083 to your computer and use it in GitHub Desktop.
using System;
static class TimeKeeper
{
public static readonly Func<DateTime> DefaultTimeKeeper = () => DateTime.UtcNow;
private static readonly object LockObject = new object();
private static Func<DateTime> CurrentTimeKeeper = DefaultTimeKeeper;
public static DateTime Current
{
get
{
lock (LockObject)
{
return CurrentTimeKeeper();
}
}
}
public static void ChangeTimeKeeper(Func<DateTime> newTimeKeeper)
{
if (newTimeKeeper == null)
{
throw new ArgumentNullException("newTimeKeeper");
}
lock (LockObject)
{
CurrentTimeKeeper = newTimeKeeper;
}
}
}
class Address : IEquatable<Address>
{
private readonly string _city;
private readonly string _country;
private readonly string _state;
private readonly string _zipCode;
private readonly string _street;
public Address(string city, string country, string state, string zipCode, string street)
{
Validate(city, "city");
Validate(country, "country");
Validate(state, "state");
Validate(zipCode, "zipCode");
Validate(street, "street");
_city = city;
_country = country;
_state = state;
_zipCode = zipCode;
_street = street;
}
public string City { get { return _city; } }
public string Country { get { return _country; } }
public string State { get { return _state; } }
public string ZipCode { get { return _zipCode; } }
public string Street { get { return _street; } }
public bool Equals(Address other)
{
if (other == null)
{
return false;
}
var result = other.City == City &&
other.Country == Country &&
other.State == State &&
other.ZipCode == ZipCode &&
other.Street == Street;
return result;
}
private static void Validate(string value, string name)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(name);
}
}
}
class Student
{
public Student(string firstName, string lastName)
{
Rename(firstName, lastName);
EnrolledOn = TimeKeeper.Current;
}
public int Id { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string CNP { get; private set; }
public DateTime EnrolledOn { get; private set; }
public Address Address { get; private set; }
public void Rename(string firstName, string lastName)
{
if (string.IsNullOrEmpty(firstName))
{
throw new ArgumentNullException("firstName");
}
if (string.IsNullOrEmpty(lastName))
{
throw new ArgumentNullException("lastName");
}
FirstName = firstName;
LastName = lastName;
}
public void ChangeAddress(Address address)
{
if (address == null)
{
throw new ArgumentNullException("address");
}
Address = address;
}
public void ChangeCNP(string cnp)
{
if (string.IsNullOrEmpty(cnp))
{
throw new ArgumentNullException("cnp");
}
CNP = cnp;
}
}
// NUnit tests
[TestFixture]
class WhenSavingStudents
{
private DbContext _context;
[SetUp]
public void SetUp()
{
_context = new DbContext("your connection string");
}
[TearDown]
public void TearDown()
{
_context.Dispose();
}
[Test]
public void ShouldGenerateId()
{
// Arrange
var student = new Student("TEST", "TESTER");
// Act
_context.Students.Insert(student);
_context.SaveChanges();
// Assert
Assert.AreNotEqual(0, student.Id);
}
[Test]
public void ShouldSaveFirstName()
{
// Arrange
var student = new Student("TEST", "TESTER");
// Act
_context.Students.Insert(student);
_context.SaveChanges();
var studentFromDatabase = _context.Students.First(x => x.Id == student.Id);
// Assert
Assert.AreEqual(student.FirstName, studentFromDatabase.FirstName);
}
[Test]
public void ShouldSaveLastName()
{
// Arrange
var student = new Student("TEST", "TESTER");
// Act
_context.Students.Insert(student);
_context.SaveChanges();
var studentFromDatabase = _context.Students.First(x => x.Id == student.Id);
// Assert
Assert.AreEqual(student.LastName, studentFromDatabase.LastName);
}
[Test]
public void ShouldSaveCNP()
{
// Arrange
var student = new Student("TEST", "TESTER");
student.ChangeCNP("TEST");
// Act
_context.Students.Insert(student);
_context.SaveChanges();
var studentFromDatabase = _context.Students.First(x => x.Id == student.Id);
// Assert
Assert.AreEqual(student.CNP, studentFromDatabase.CNP);
}
[Test]
public void ShouldSaveEnrolledOn()
{
// Arrange
var student = new Student("TEST", "TESTER");
// Act
_context.Students.Insert(student);
_context.SaveChanges();
var studentFromDatabase = _context.Students.First(x => x.Id == student.Id);
// Assert
Assert.AreEqual(student.EnrolledOn, studentFromDatabase.EnrolledOn);
}
[Test]
public void ShouldSaveAddress()
{
// Arrange
var student = new Student("TEST", "TESTER");
var address = new Address("A", "B", "C", "D", "E");
// Act
_context.Students.Insert(student);
_context.SaveChanges();
var studentFromDatabase = _context.Students.First(x => x.Id == student.Id);
// Assert
Assert.AreEqual(student.Address, studentFromDatabase.Address);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment