From 73abcf9d65abda03c91d45f06832c3f939de739b Mon Sep 17 00:00:00 2001 From: "marcin.celej" Date: Fri, 2 Sep 2016 23:37:04 +0200 Subject: [PATCH] Improve Unit Tests #6 - some unit tests improvements --- .../Failures/FailCastableTest.cs | 57 ++++++++++++++++--- .../Failures/FailDateTimeTest.cs | 26 +++++++-- 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/Synergy.Contracts.Test/Failures/FailCastableTest.cs b/Synergy.Contracts.Test/Failures/FailCastableTest.cs index 1377024..2c1b97f 100644 --- a/Synergy.Contracts.Test/Failures/FailCastableTest.cs +++ b/Synergy.Contracts.Test/Failures/FailCastableTest.cs @@ -25,12 +25,24 @@ public void AsOrFail() } [Test] - [TestCase("text")] - [TestCase(null)] - public void AsOrFailSuccess(string toCast) + public void AsOrFailSuccess() { + // ARRANGE + object somethingCastable = "text"; + + // ACT + somethingCastable.AsOrFail(); + } + + [Test] + public void AsOrFailSuccessWithNull() + { + // ARRANGE + object somethingCastable = null; + // ACT - toCast.AsOrFail(); + // ReSharper disable once ExpressionIsAlwaysNull + somethingCastable.AsOrFail(); } #endregion @@ -40,15 +52,42 @@ public void AsOrFailSuccess(string toCast) [Test] public void CastOrFail() { - Assert.Throws( - () => new object().CastOrFail() + // ARRANGE + object somethingNotCastable = 1; + + // ACT + var exception = Assert.Throws( + () => somethingNotCastable.CastOrFail() ); - Assert.Throws( - () => ((object) null).CastOrFail() + // ASSERT + Assert.That(exception.Message, Is.EqualTo("Expected object of type 'System.String' but was '1'")); + } + + [Test] + public void CastOrFailWithNull() + { + // ARRANGE + object somethingNotCastable = null; + + // ACT + var exception = Assert.Throws( + // ReSharper disable once ExpressionIsAlwaysNull + () => somethingNotCastable.CastOrFail() ); - "tekst".CastOrFail(); + // ASSERT + Assert.That(exception.Message, Is.EqualTo("Expected object of type 'System.String' but was 'null'")); + } + + [Test] + public void CastOrFailSuccess() + { + // ARRANGE + var somethingCastable = "text"; + + // ACT + somethingCastable.CastOrFail(); } #endregion diff --git a/Synergy.Contracts.Test/Failures/FailDateTimeTest.cs b/Synergy.Contracts.Test/Failures/FailDateTimeTest.cs index 321946b..2e8dbad 100644 --- a/Synergy.Contracts.Test/Failures/FailDateTimeTest.cs +++ b/Synergy.Contracts.Test/Failures/FailDateTimeTest.cs @@ -10,14 +10,30 @@ public class FailDateTimeTest [Test] public void IfNotMidnight() { - DateTime now = DateTime.Today.AddSeconds(1000); + // ARRANGE + DateTime notMidnight = DateTime.Today.AddSeconds(1000); - Assert.Throws( - () => Fail.IfNotMidnight( now, "ojtam ojtam") + // ACT + var exception = Assert.Throws( + () => Fail.IfNotMidnight(notMidnight, "date should have no hour nor second") ); - Fail.IfNotMidnight( DateTime.Today, "ojtam ojtam"); - Fail.IfNotMidnight(null, "ojtam ojtam"); + // ASSERT + Assert.That(exception.Message, Is.EqualTo("date should have no hour nor second")); + } + + [Test] + public void IfNotMidnightSuccess() + { + // ACT + Fail.IfNotMidnight(DateTime.Today, "date should have no hour nor second"); + } + + [Test] + public void IfNotMidnightSuccessWithNull() + { + // ACT + Fail.IfNotMidnight(null, "date should have no hour nor second"); } [Test]