shouldly
TRANSCRIPT
Starting Point
//Arrangevar target = new Calculator();
//Actvar actual = target.Sqrt(9.0);
//AssertAssert.AreEqual(actual, 3.0);
Shouldly
//Arrangevar target = new Calculator();
//Actvar actual= target.Sqrt(9.0);
//Assertactual.ShouldBe(3.0);
Shouldly + Readability
//Arrangevar calculator = new Calculator();
//Actvar sqrt_of_nine = calculator.Sqrt(9.0);
//Assertsqrt_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
ReallyAssert.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 ReallyAssert.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 SugarShouldBeShouldNotBeShouldBeOneOfShouldNotBeOneOfShouldBeGreaterThanShouldBeLessThanShouldBeOfTypeShouldBeAssignableToShouldBeInRangeShouldNotBeInRangeShouldAllBeShouldContainShouldNotContainShouldBeEmptyShouldNotBeEmpty
ShouldBeOneOfShouldBeSubsetOfShouldContainKeyShouldNotStartWithShouldEndWithShouldNotEndWithShouldMatchShouldBeNullOrEmptyShouldNotBeNullOrEmptyShouldContainShouldNotContainShouldContainWithoutWhitespaceShouldThrowShouldHavePropertyCompleteIn…
Example 1/3 - ShouldBeOneOfvar 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 - ShouldSatisfyAllConditionsvar 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.
Thank you!
Shouldly:
github.com/shouldly/shouldlynuget:
Let’s keep in touch:
twitter.com/urig