(that we call 'checks' BTW, to avoid conflicts with all your favorite test runners)
Check.That()
is all you've got to remember! before you get carried away by the super-duper-happy-DOT experience of NFluent (i.e. the IntelliSense experience)
var integers = new int[] { 1, 2, 3, 4, 5, 666 }; Check.That(integers).Contains(3, 5, 666); integers = new int[] { 1, 2, 3 }; Check.That(integers).IsOnlyMadeOf(3, 2, 1); var guitarHeroes = new[] { "Hendrix", "Paco de Lucia", "Django Reinhardt", "Baden Powell" }; Check.That(guitarHeroes).ContainsExactly("Hendrix", "Paco de Lucia", "Django Reinhardt", "Baden Powell"); var camus = new Person() { Name = "Camus" }; var sartre = new Person() { Name = "Sartre" }; Check.That(camus).IsNotEqualTo(sartre).And.IsInstanceOf<Person>(); var heroes = "Batman and Robin"; Check.That(heroes).Not.Contains("Joker").And.StartsWith("Bat").And.Contains("Robin"); int? one = 1; Check.That(one).HasAValue().Which.IsPositive().And.IsEqualTo(1); const Nationality FrenchNationality = Nationality.French; Check.ThatEnum(FrenchNationality).IsNotEqualTo(Nationality.Korean); string motivationalSaying = "Failure is mother of success."; Check.That(motivationalSaying).IsNotInstanceOf<int>();With NFluent, you can also write checks like this:
var persons = new List<Person> { new Person { Name = "Thomas", Age = 38 }, new Person { Name = "Achille", Age = 10, Nationality = Nationality.French }, new Person { Name = "Anton", Age = 7, Nationality = Nationality.French }, new Person { Name = "Arjun", Age = 7, Nationality = Nationality.Indian } }; Check.That(persons.Extracting("Name")).ContainsExactly("Thomas", "Achille", "Anton", "Arjun"); Check.That(persons.Extracting("Age")).ContainsExactly(38, 10, 7, 7); Check.That(persons.Extracting("Nationality")).ContainsExactly(Nationality.Unknown, Nationality.French, Nationality.French, Nationality.Indian); // more fluent than the following classical NUnit way, isn't it? // CollectionAssert.AreEquivalent(persons.Extracting("Age"), new[] { 38, 10, 7, 7 }); // it's maybe even more fluent than the java versions // FEST fluent assert v 2.x: // assertThat(extractProperty("name" , String.class).from(inn.getItems())).containsExactly("+5 Dexterity Vest", "Aged Brie", "Elixir of the Mongoose", "Sulfuras, Hand of Ragnaros", "Backstage passes to a TAFKAL80ETC concert", "Conjured Mana Cake"); // FEST fluent assert v 1.x: // assertThat(inn.getItems()).onProperty("name").containsExactly("+5 Dexterity Vest", "Aged Brie", "Elixir of the Mongoose", "Sulfuras, Hand of Ragnaros", "Backstage passes to a TAFKAL80ETC concert", "Conjured Mana Cake");Or like this:
// Works also with lambda for exception checking Check.ThatCode(() => { throw new InvalidOperationException(); }).Throws<InvalidOperationException>(); // or execution duration checking Check.ThatCode(() => Thread.Sleep(30)).LastsLessThan(60, TimeUnit.Milliseconds);
With truly-helpful error messages provided, NFluent spares you a few sessions with the debugger in your day to day work
Another booster for your productivity, and to reduce your TDD feedback loop ;-)
Indeed, NFluent is less error-prone for your day to day work.
Here, the distinction between the expected value and the one to be checked is natively enforced by the NFluent syntax. Thus, no question to ask ourselves when we write our assertions. And no question to ask ourselves when consulting the error messages on our Software Factory dashboard.
Very close to plain English, the NFluent syntax makes your tests easier to read even for non-developers people (QA, BA)
You wanna try? Follow the white rabbit here, or read the nice article published by Rui on CodeDistillers' blog.
. . .
Indeed, NFluent is simply an awesome assertion library, and ... she's not exclusive ;-)
There were attempts made previously by other open source projects, but even the most serious alternative did not suit our needs.
Indeed:
.Should()
.... why don't they choose Must instead?!?). And thus, you'd rather rely on a stronger semantic for your checks (i.e. NFluent's Check.That
).var myArray = new[] { 1, 2, 3 }; myArray.Should().Contain(item => item > 3, "at least {0} item should be larger than 3", 1);
Check.That(myArray).IsGreaterThan(3);
new[] {...}
as a method argument like does another famous "fluent" library?
myArray.Should().Have.SameSequenceAs(new[] { 1, 2, 3 });
Check.That(myArray).ContainsExactly(1,2,3);