Please enable JavaScript to view this site.

Codolex

When adding code, it's always important to use unit tests. With the example project, a unit test and integration test project are provided. We will use both projects. The unit test to test the validator, and the integration to check if the generated code can be compiled.

 

Unit test

The unit test project also has an example file that we need to remove, copy and add to the project.

- "Codolex.Activity.YourActivityName.Tests.pas"

Rename this file the same way as with the components files.

The test project uses the DUnitX TestFramework to perform the unit tests. Assuming there was no problem with copying and adding the new files, we don't need to worry about how this works.

 

By default, three things are tested in the unit test

1.If the creation of the component/activity does not raise an error.

2.If the codegen does not raise an error when called.

3.If the validation does not raise an error by default.

 

It's recommended to keep these tests in the project, but also expand on them. In our project, we're going to check if the validator indeed does raise an error if there is no SoundFile.

To add some extra tests, add a procedure with the [Test] directive

 

...

public

   [Setup]

  procedure Setup; override;

 

   [Test]

  procedure TestCreateComponent;

 

   [Test]

  procedure TestCodeGenAvailable;

 

   [Test]

  procedure TestValidatorAvailable;

 

   [Test]

  procedure Validation;

end;

This procedure will be executed when the tests are executed.

 

In this article, we won't dive too deep into the options of the test and validators, but a good place to start is to copy this line to use the validator. The .Validator function at the end of the line expects an activity, this is the activity we just created to test, but can be any activity. You can also add multiple activities to test in combination.

 

var Validator := (FPluggable as IFlowPluggableComponentWithValidation).Validator(SoundFileActivity);

The first thing to test is if the validator does indeed return an error when no SoundFile is provided

 

var SoundFileActivity: IFlowActivityPlaySound;

SoundFileActivity := TFlowActivityPlaySound.Create;

 

var Validator := (FPluggable as IFlowPluggableComponentWithValidation).Validator(SoundFileActivity);

var ValidationResults := Validator.Validate;

 

Assert.IsTrue((ValidationResults.Count = 1), 'Validation has no result when soundfile is empty');

The assert class contains many static functions to assert something with. In the example, we used it to check if there are any validation results at all, But you can also create the expected result and check if it's in the validation results with the .Contains function. Use the autocomplete to see the functions available. If something does not pass the check, an error is thrown when executing the unit tests.

 

uses

 ProjectModel.Variable.Interfaces,

 ProjectModel.Variable,

...

var Variable: IVariable := TVariable.Create;

 

SoundFileActivity.SoundFile := Variable;

ValidationResults := Validator.Validate;

 

Assert.IsTrue((ValidationResults.Count = 0), 'Validation has error when component is filled correctly');

To complete the test, we're going to create a flow variable, assign it to the SoundFile, and run the validator again. After that, we can check if the result is 0.

 

Integration Tests

In the integration test, we are going to test the code directly with a project. Remove the example file and add a copied file for the activity as well. Select the project and open the Codolex Project Explorer. Create a new test project, or select an existing one if you already tested with a project.

 

Add a module, flow class, and flow to the project, and use the created activity in the flow.

 

If you don't find your activity in the activity palette yet, be sure that the core**0.bpl is built and copied to your appdata folder (%appdata%\Codolex\BPL). Restart Delphi if needed.Enter topic text here.

 

17291114357789

After you build your flow, you can use it in the test project with the "Use in code button" in the simple test function.

 

17290508138653

 

After that, you can simply run the integration test project, and see if any errors pop up.

If you are not able to build the project, something might be wrong with the generated code. If you can run the project, but the test fails, the generated code results in an error.

 

© by GDK Software