Using stubs

Component Testing for C

Use the STUB statement to declare that you want to use a stub rather than the original function. You can use the STUB instruction within environments or test scenarios.

This STUB instruction tests input parameters and assigns a value to output parameters each time the simulated function is called.

The following information is required for every stub called in a scenario:

  • Test values for the input parameters

  • Return values for the output parameters

  • Test and return values for the input/output parameters

  • Where appropriate, the return value of the called stub

Example

The following example illustrates use of a stub which simulates file access.

SERVICE copy_file

#char file1[100], file2[100];

#int s;

TEST 1

FAMILY nominal

ELEMENT

VAR file1, init = "file1", ev = init

VAR file2, init = "file2", ev = init

VAR s, init == , ev = 1

STUB open_file ("file1")3

STUB create_file ("file2")4

STUB read_file (3,"line 1")1, (3,"line 2")1, (3,"")0

STUB write_file (4,"line 1")1, (4,"line 2")1

STUB close_file (3)1, (4)1

#s = copy_file(file1, file2);

END ELEMENT

END TEST

END SERVICE

The following example specifies that you expect three calls of foo.

STUB STUB1.foo(1)1, (2)2, (3)3

...

#foo(1);

#foo(2);

#foo(4);

The first call has a parameter of 1 and returns 1. The second has a a parameter of 2 and returns 2 and the third has a parameter of 3 and returns 3. Anything that does not match is reported in the test report as a failure.