◷ 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,
- Simply create a unit test project as part of your solution in Visual Studio.
- Embed your license in the Unit Test project
- 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:
- Loads a logic
- Then passes your data to the engine
- 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);
}