Unit test

From FlexRule Wiki
Jump to: navigation, search

Applying License

During unit testing, you need to specify where and how to read the license file. In order to do that, you can simply use the technique that we mentioned about custom location for license file yourself.

Or add a file named UnitTestLicenseProvider.cs to your unit test project and copy the code bellow:

  1. using System;
  2. using System.IO;
  3. using FlexRule.License;
  4.  
  5. [assembly: LicenseProvider(typeof(UnitTestLicenseProvider))]
  6.  
  7. namespace FlexRule.License.UnitTests
  8. {
  9.     /// <summary>
  10.     /// This class reads the license file for the project
  11.     /// </summary>
  12.     public class UnitTestLicenseProvider : ILicenseProvider
  13.     {
  14.         public string ReadLicense()
  15.         {
  16.             string yourLicense="..."; // replace ... with your license content
  17.             return yourLicense;
  18.         }
  19.     }
  20. }

Initilizing License

When custom provider is defined, for initializing license, just call the default override wth no parameters:

  1. UserLicense.Initialize()

Samples

Simply write your a test that

  1. Loads a logic
  2. Then pass your data to engine
  3. Assert on the result

Here are some example on writing unit tests:

  1. [TestMethod]
  2. public void when_compuer_has_normal_case_powersupply_isnotallowed_must_pass_test_ruleengine()
  3. {
  4.     var order = new Order();
  5.     order.Lines.Add(new OrderLine { Product = { Name = "Case" } });
  6.     order.Lines.Add(new OrderLine { Product = { Name = "Sound Card" } });
  7.  
  8.     var engine = RuntimeEngine.FromXml(File.OpenRead("ComputerProduct_CaseCannotHaveExtra.xml"));
  9.     var result = engine.Run(new RunParameter("Order", order));
  10.  
  11.     Assert.IsNotNull(result.Outcome);
  12.     Assert.IsTrue(result.Outcome.Value);
  13. }

Another example of testing different type of logic:

  1. [TestMethod]
  2. public void define_potential_theft_rating()
  3. {
  4.     var dt = RuntimeEngine.FromSpreadSheet(File.OpenRead(@"UServ Product Derby.xlsx"), "PotentialTheftRating");
  5.     var car = new Car()
  6.     {
  7.         Price = 27000,
  8.         Made = "Hyundai",
  9.         Model = "IX35",
  10.     };
  11.  
  12.     dt.Run(probList, car);
  13.  
  14.     Assert.AreEqual(PotentialTheftCategory.High, car.TheftCategory);
  15. }