Blog

All Blog Posts  |  Next Post  |  Previous Post

Unit testing comes to TMS WEB Core

Bookmarks: 

Friday, December 10, 2021

Let's come straight to the point. Writing unit tests is a chore and having unit tests is a blessing. We all want to have the blessing, so with integrated unit testing in TMS WEB Core, we tried to minimize the chore.

For unit testing to cover real usage scenarios, it is necessary to run the unit tests on the 'operating system' where the web application will run, that is the web browser. After all, all TMS WEB Core apps, and thus your code, will run in the browser, will involve dealing with the browser DOM and browser APIs, it is only logical that a TMS WEB Core unit test runs as a web app in the browser. It also means having the ability to deal with a typical web app world phenomenon and that is asynchronous behavior. 

Introducing unit testing now integrated from TMS WEB Core v1.9.5.0.

To start a new unit test project, create this from the Delphi IDE wizard and it creates a test project with one unit containing one test class with one test method. It allows you to fully focus on writing the test classes with their test methods.
TMS Software Delphi  Components

In the project manager, it looks like:

TMS Software Delphi  Components

and the code generated is:

TMS Software Delphi  Components

Notice in the code the attribute [TestFixture] on the test classes and the attribute [Test] on the test methods. A class can obviously contain as much test methods as you want and you can register as much test classes as you want with a call to TTMSWEBUnitTestingRunner.RegisterClass(). For the feedback when running the test application, use the Assert class. 

When running this application, the test application in the browser behaves in the following way:
TMS Software Delphi  Components

Asynchronous behavior in the browser

Now, how can we deal with typical asynchronous behavior in the browser and still test it properly? Well, for any asynchronous behavior in the browser, decorate the test method using it with the [Async] attribute. Then you can use the await() function to wait for the asynchronous response of the call.  This sample code demonstrates this:

TMyTestClassUnit1 = class(TObject)
published
  //This is an async test method
  [Test] [async]
  procedure TestAsync;
end;

procedure TMyTestClassUnit1.TestAsync;
var
  wr: TWebHttpRequest;
  res: TJSXMLHttpRequest;
  js: TJSONObject;
begin
  wr := TWebHttpRequest.Create(nil);

  wr.URL := 'https://download.tmssoftware.com/tmsweb/1.json';

  res := await(TJSXMLHttpRequest, wr.Perform);

  js := TJSONObject(TJSONObject.ParseJSONValue(res.responseText));

  // response value for userId should be '1'
  Assert.AreEqual(js.GetJSONValue('userId'),'1');
end;


Testing JavaScript directly

When you use external JavaScript libraries that you also want to involve in a test, there are several ways you can do this. You might have an Object Pascal wrapper class for JavaScript objects that you use in your test method but it could be as simple as calling the JavaScript directly from an ASM code block (a block with embedded JavaScript code within your Object Pascal code). 

This is a very simple example of directly calling the JavaScript window prompt function to get a user input:

procedure TMyTestClassUnit2.TestIntToStr;
var
  s: string;
begin
  asm
    s = window.prompt('Please give your name');
  end;

  Assert.AreEqual('Bruno', s);
end;

The result in the browser when running this test becomes:

TMS Software Delphi  Components

A video introduction

Follow unit testing step by step as our colleague Holger Flick explains in this YouTube video:



Get started

Go to the next level of application development with TMS WEB Core now. Bring your applications as always up-to-date, zero deployment, modern HTML5/CSS3 GUI web client apps or as cross-platform desktop apps for Windows, macOS, Linux, Raspberry Pi. Supercharge your Delphi IDE with TMS WEB Core or use TMS WEB Core with Visual Studio Code directly from Windows, macOS or Linux development machines.








Bruno Fierens


Bookmarks: 

This blog post has received 3 comments.


1. Saturday, December 11, 2021 at 9:28:16 AM

Is it planned to make this project type available also for VSC?

Ulrich Groffy


2. Saturday, December 11, 2021 at 6:11:04 PM

It is in the works. Expect it soon.

Bruno Fierens


3. Tuesday, December 21, 2021 at 6:56:24 PM

Its in the new version 1.9.5316 - you forgot to mention it in the relase notes :)

Ulrich Groffy




Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post