stringdelphiaccess-violationdunit

Delphi pass string to procedure gets an access violation error


I am currently trying to create a test procedure and pass it a string. And the function checks the string for equivalence. The problem is that when the test runs, I get a access violation error of EAccessViolation on the string that I pass inside the Procedure. I understand that this is a memory declaration problem but I am not sure how to fix it.

Here is my code:

Declare

TestTForm1 = class(TTestCase)
strict private
    FForm1: TForm1;
public
    procedure SetUp; override;
    procedure TearDown; override;
published
    procedure TestCompareListBoxToFile(Method : String);
end;

Call

TestCompareListBoxToFile('Save');

Procedure

procedure TestTForm1.TestCompareListBoxToFile(Method : String);  
begin
    Check('Save' = Method,'they dont match');
end;

I am new to delphi so if there is anything I am missing, please let me know. please be specific in the response. Thank you.


Solution

  • That code is called by the unit test runner. It uses RTTI to find published methods whose names begin with 'Test'. On the face of it, this appears to be a DUnit test case.

    The runner expects a procedure that accepts no parameters, and calls the method as such. You on the other hand, provide a method that does require a parameter. A parameter that is not provided. Hence the runtime error.

    Now, somewhere in your code you say that you are calling the method like this:

    TestCompareListBoxToFile('Save');
    

    But that's just not how tests are invoked. Tests are invoked by the runner which uses RTTI to do so. If you attempt to call the function as well, that's just wrong.

    You must declare your method like this:

    procedure TestCompareListBoxToFile;
    

    You probably need to go back to the documentation and examples for the unit test framework and learn how to design your test case to be able to accept parameters. In fact, before even doing that, I suggest you go right back to basics and make sure you fully understand how the runner discovers your tests and then runs them.