Skip to content

Commit

Permalink
Improve Unit Tests #6
Browse files Browse the repository at this point in the history
- some unit tests improvements
  • Loading branch information
MarcinCelej committed Sep 2, 2016
1 parent 509f743 commit 73abcf9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
57 changes: 48 additions & 9 deletions Synergy.Contracts.Test/Failures/FailCastableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
}

[Test]
public void AsOrFailSuccessWithNull()
{
// ARRANGE
object somethingCastable = null;

// ACT
toCast.AsOrFail<string>();
// ReSharper disable once ExpressionIsAlwaysNull
somethingCastable.AsOrFail<string>();
}

#endregion
Expand All @@ -40,15 +52,42 @@ public void AsOrFailSuccess(string toCast)
[Test]
public void CastOrFail()
{
Assert.Throws<DesignByContractViolationException>(
() => new object().CastOrFail<string>()
// ARRANGE
object somethingNotCastable = 1;

// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
() => somethingNotCastable.CastOrFail<string>()
);

Assert.Throws<DesignByContractViolationException>(
() => ((object) null).CastOrFail<string>()
// 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<DesignByContractViolationException>(
// ReSharper disable once ExpressionIsAlwaysNull
() => somethingNotCastable.CastOrFail<string>()
);

"tekst".CastOrFail<string>();
// 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<string>();
}

#endregion
Expand Down
26 changes: 21 additions & 5 deletions Synergy.Contracts.Test/Failures/FailDateTimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DesignByContractViolationException>(
() => Fail.IfNotMidnight( now, "ojtam ojtam")
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
() => 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]
Expand Down

0 comments on commit 73abcf9

Please sign in to comment.