Shouldly
Asserting, the way it should be
Uri Goldstein (github/twitter: @urig)
Unit Tests
1. Arrange
2. Act
3. Assert
Starting Point
//Arrange
var target = new Calculator();
//Act
var actual = target.Sqrt(9.0);
//Assert
Assert.AreEqual(actual, 3.0);
Shouldly
//Arrange
var target = new Calculator();
//Act
var actual= target.Sqrt(9.0);
//Assert
actual.ShouldBe(3.0);
Shouldly + Readability
//Arrange
var calculator = new Calculator();
//Act
var sqrt_of_nine = calculator.Sqrt(9.0);
//Assert
sqrt_of_nine.ShouldBe(3.0);
Shouldly + Readability = Win!
Assert.AreEqual(actual, 3.0);
sqrt_of_nine.ShouldBe(3.0);
Assert.AreEqual failed. Expected:<3>.
Actual:<4>.
sqrt_of_nine
should be
3
but was
4
Really
Assert.That(names.IndexOfValue("Uri"), Is.EqualTo(2));
names.IndexOfValue("Uri").ShouldBe(2);
Expected 2 but was 1
names.IndexOfValue("Uri")should be
2
but was
1
Really Really
Assert.AreEqual(new[] { 1, 2, 3 }, new[] { 1, 2, 4 });
(new[] { 1, 2, 3 }).ShouldBe(new[] { 1, 2, 4 });
Expected:<System.Int32[]>.
Actual:<System.Int32[]>.
new[] { 1, 2, 3 })
should be
[1, 2, 4]
but was
[1, 2, 3]
difference
[1, 2, *3*]
Syntactic Sugar
ShouldBe
ShouldNotBe
ShouldBeOneOf
ShouldNotBeOneOf
ShouldBeGreaterThan
ShouldBeLessThan
ShouldBeOfType
ShouldBeAssignableTo
ShouldBeInRange
ShouldNotBeInRange
ShouldAllBe
ShouldContain
ShouldNotContain
ShouldBeEmpty
ShouldNotBeEmpty
ShouldBeOneOf
ShouldBeSubsetOf
ShouldContainKey
ShouldNotStartWith
ShouldEndWith
ShouldNotEndWith
ShouldMatch
ShouldBeNullOrEmpty
ShouldNotBeNullOrEmpty
ShouldContain
ShouldNotContain
ShouldContainWithoutWhitespace
ShouldThrow
ShouldHaveProperty
CompleteIn
…
Example 1/3 - ShouldBeOneOf
var apu = new Person() { Name = "Apu" };
var homer = new Person() { Name = "Homer" };
var skinner = new Person() { Name = "Skinner" };
var barney = new Person() { Name = "Barney" };
var theBeSharps = new List<Person>() { homer, skinner, barney };
apu.ShouldBeOneOf(theBeSharps.ToArray());
Apu
should be one of
[Homer, Skinner, Barney]
but was
Apu
Source: http://docs.shouldly-lib.net/v2.4.0/docs/shouldbeoneof
Example 2/3 - ShouldSatisfyAllConditions
var millionaire = new Person() { Name = "Homer", Salary = 30000 };
millionaire.ShouldSatisfyAllConditions
(
() => millionaire.Name.ShouldBe("Mr.Burns"),
() => millionaire.Salary.ShouldBeGreaterThan(1000000)
);
millionaire should satisfy all the conditions specified, but does not.
The following errors were found ...
--------------- Error 1 ---------------
millionaire.Name
should be
"Mr.Burns"
but was
"Homer"
--------------- Error 2 ---------------
millionaire.Salary
should be greater than
1000000
but was
30000
-----------------------------------------
Source: http://docs.shouldly-lib.net/v2.4.0/docs/shouldsatisfyallconditions
Example 3/3 - ShouldThrow (Async)
Should.Throw<DivideByZeroException>(() =>
{
return Task.Factory.StartNew(() =>
{ var y = homer.Salary/denominator; });
return task;
});
Should
throw
System.DivideByZeroException
but does not
http://docs.shouldly-lib.net/v2.4.0/docs/shouldthrow-2
Shouldly
• Make your tests more readable.
• Fail faster with excellent error messages.
• Bonus: Async, Multi-conditions.
• Free Open Source Software.
http://up-for-grabs.net/
Just Jump In
Thank you!
Shouldly:
github.com/shouldly/shouldly
nuget:
Let’s keep in touch:
twitter.com/urig

Shouldly