I'm starting with DUnit tests so sorry in advance for any newbie mistakes. When I tried to run simple tests and the compiler runs the SetUp of my test class, it stops returning the message 'Cannot create form. No MDI forms are currently active'.
After some research, I found some explanations but none related to my problem.
The problem appears to be when I call the Create method of my child origin class - by origin I mean the class where I have all the procedures and functions to be tested, lets call it by TfrmFoo. And my test class should be the TestTfrmFoo. The TFrmFoo has a parent with some basic codes, the TfrmParentFoo.
I'm calling the Create method like this:
...
implementation
procedure TestTfrmFoo.SetUp;
begin
FfrmParentFoo := TfrmParentFoo.Create(nil);
FfrmFoo := TfrmFoo.Create(FfrmParentFoo);
end;
procedure TestTfrmFoo.TearDown;
begin
FfrmFoo.Free;
FfrmFoo := nil;
end;
...
After that, the Create method of the TfrmFoo class is called and the following error occurs: 'Cannot create form. No MDI forms are currently active'.
I already tried to override the Create of my child class TfrmFoo setting the FormStyle property to 'fsNormal' instead 'fsMDIChild' and nothing. I also found some ideas about using a sleep(500) but the problem continues.
Edited:
After the Remy Lebeau
contribution, I changed the SetUp test to create the parent form before calling the child, and using it as a parameter to the Create child. Same error message. Did I change something wrong?
Any other tips? tks!
When a TForm
has its FormStyle
set to fsMDIChild
, the project MUST have an Application.MainForm
created whose FormStyle
is set to fsMDIForm
. This is a hard-coded requirement of the VCL, not the underlying MDI system (though, there is a way to circumvent this limitation with some manual work).
The error you are seeing means your project does not have an Application.MainForm
created and set to fsMDIForm
before any fsMDIChild
forms are created. You need to fix that.
Setting the child form's FormStyle
in its constructor or OnCreate
event is too late, because the error happens when the child form creates its HWND, which occurs early in the DFM streaming process, before Create
/OnCreate
has an oppurtunity to try assigning the FormStyle
.