public void SampleUnitTestWithoutSetup() { When.A <object>("has its toString method called", (o) => // o is an instance of the generic // type passed to When.A. In this case // its just an instance of an object { o.ToString(); }) .TheTest .ShouldPass(because => { // this is code, no way to make it fall in line, though there are other // ways to get at the object under test object testObj = because.ObjectUnderTest <object>(); // this is an assertion because.ItsTrue(/* success message */ "the object under test was not null", testObj != null, /* failure message */ "the object under test was null"); because.ItsTrue(/* success message */ "Big-Bird is KLGates's favorite", true, /* failure message */ "big bird is not cool"); because.ItsTrue(/* success message */ "Oscar-the-Grouch is a very grouchy guy", true, /* failure message */ "Oscar-the-Grouch is happy"); }) .SoBeHappy(c => { // no setup so nothing to clean up. // clean up isn't always necessary. }) .UnlessItFailed(); }
public void TestFailure() { When.A <object>("is instantiated", (o) => { }) .TheTest .ShouldPass(because => { because.ItsFalse("the test passed", true, "the test failed"); }) .SoBeHappy() .UnlessItFailed("The test failed, but in this case that's a good thing since that's what we're testing"); }
public void ClassifierTest() { SQLiteRegistrar.Register <Feature>(); Db.EnsureSchema <Feature>(); When.A <NaiveBayesClassifier>("is trained", (classifer) => { classifer.Train("The quick brown fox jumps over the lazy dog", "good"); classifer.Train("make quick money in the online casino", "bad"); }) .TheTest .ShouldPass(because => { Classifier classifier = because.ObjectUnderTest <Classifier>(); long quickGood = classifier.FeatureCount("quick", "good"); long quickBad = classifier.FeatureCount("quick", "bad"); because.ItsTrue("The good count of 'quick' is " + quickGood, quickGood > 0); because.ItsTrue("The bad count of 'quick' is " + quickBad, quickBad > 0); }) .SoBeHappy(c => { }) .UnlessItFailed(); }