Example of how StringFixture works

3.2k views Asked by At

Can someone provide an example of how to use FitNesse String Fixture?

The following doesn't work:

|fitnesse.fixtures.StringFixture|
|value|expected|contains?       |
|hello|lo      |true            |

To provide context I'm trying to verify the value of the Id field of a output of a REST call:

|Table:smartrics.rest.fitnesse.fixture.RestFixture | http://localhost|
|GET|/data/1234|  |                                                | |
|let|body      |js| JSON.parse(response.body)                      | |
|let|id        |js| JSON.parse(response.body).Data[0].Id           | |

And I'd like not to write any code for this at all if possible.

2

There are 2 answers

0
Mike Stockdale On

Looks like contains() returns expected, not true/false. So the test would be:

|fitnesse.fixtures.StringFixture|
|value|expected|contains?|
|hello|lo|lo|

Not sure this is a useful fixture, looks like it's just a demonstration sample.

2
Fried Hoeben On

I don't know about the fixtures you refer to, but you can do what you describe with my fixtures (https://github.com/fhoeben/hsac-fitnesse-fixtures).

For strings: nl.hsac.fitnesse.fixture.slim.StringFixture

|script|string fixture                                                                                    |
|check |value of             |Hello                      |Hello                                           |
|check |length of            |Hello                      |5                                               |
|ensure|text                 |Hello world!               |contains    |world                              |
|reject|text                 |Hello world!               |contains    |moon                               |
|ensure|value                |Hello                      |differs from|Hell                               |
|ensure|value                |Hello                      |equals      |Hello                              |
|check |convert to upper case|Bye                        |BYE                                             |
|check |convert to lower case|Bye                        |bye                                             |
|check |normalize whitespace |!-   Hello    World    -!  |Hello World                                     |
|check |replace all          |Hello ([A-Za-z]+)          |in          |Hello World|with|$1      |World    |
|check |replace all          |(\d{4})-(\d{1,2})-(\d{1,2})|in          |1975-12-3  |with|$3-$2-$1|3-12-1975|

For JSON (which uses JsonPath): nl.hsac.fitnesse.fixture.slim.JsonHttpTest

|script  |json http test                            |
|get from|http://echo.jsontest.com/key/value/one/two|
|show    |response                                  |
|check   |json path       |$.key       |value       |
|check   |json path       |$.one       |two         |

So your REST check would be something like:

|script  |json http test                 |
|get from|http://localhost/data/1234     |
|show    |response                       |
|check   |json path      |$.Data[0].Id |1|
|note    |or assign to symbols           |
|$body=  |response                       |
|$id=    |json path      |$.Data[0].Id   |