Unit Test

◷ Reading Time: 2 minutes

There are scenarios you want to integrate some Unit Tests as part of your CI/CD or any of your development process. To do that so,

  1. Simply create a unit test project as part of your solution in Visual Studio.
  2. Embed your license in the Unit Test project
  3. Write your unit test using In-Proc integration of RuntimeEngine or CommandLine Handler

Writing Unit Test

To write a unit test that runs your FlexRule logic, simply:

  1. Loads a logic
  2. Then passes your data to the engine
  3. Asserts on the result

Here are some example of writing unit tests:

[TestMethod]
public void when_compuer_has_normal_case_powersupply_isnotallowed_must_pass_test_ruleengine()
{
    var order = new Order();
    order.Lines.Add(new OrderLine { Product = { Name = "Case" } });
    order.Lines.Add(new OrderLine { Product = { Name = "Sound Card" } });
 
    var engine = RuntimeEngine.FromXml(File.OpenRead("ComputerProduct_CaseCannotHaveExtra.xml"));
    var result = engine.Run(new RunParameter("Order", order));
 
    Assert.IsNotNull(result.Outcome);
    Assert.IsTrue(result.Outcome.Value);
}

Another example of testing different types of logic:

[TestMethod]
public void define_potential_theft_rating()
{
    var dt = RuntimeEngine.FromSpreadSheet(File.OpenRead(@"UServ Product Derby.xlsx"), "PotentialTheftRating");
    var car = new Car()
    {
        Price = 27000,
        Made = "Hyundai",
        Model = "IX35",
    }; 
    dt.Run(probList, car);
 
    Assert.AreEqual(PotentialTheftCategory.High, car.TheftCategory);
}
Updated on April 17, 2020

Was this article helpful?

Related Articles