Skip to content
This repository was archived by the owner on Sep 12, 2022. It is now read-only.

Commit 50e1718

Browse files
committed
Update README to include templating usage
1 parent f084334 commit 50e1718

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ To add a fixture all you have to do is call one of the `addFixture` methods in t
118118
serverRule.addFixture(200, "body.json");
119119
```
120120

121-
This will add a response with status code 200 and the contents of the file `body.json` as the body. This file, by default, must be located in a folder with name `fixtures`. This folder works different for Local Tests and Instrumented Tests.
121+
This will add a response with status code 200 and the contents of the file `body.json` as the body. This file, by default, must be located in a folder with name `fixtures`. This folder works differently for Local Tests and Instrumented Tests.
122122

123123
- Local tests: these are run locally in the JVM (usually with Robolectric) and follow different conventions. Your source folder `test` may contain a folder `java` and a folder `resources`. When you compile your code it takes everything in the `resources` folder and puts in the root of your `.class` files. So, your fixtures folder must go inside `resources` folder.
124-
- Instrumented tests: there are run in a device or emulator (usually with Espresso or Robotium). It follows the android folder layout and so you may have an assets folder inside your `androidTest` folder. Your `fixtures` folder must go there.
124+
- Instrumented tests: these are run on a device or emulator (usually with Espresso or Robotium). It follows the android folder layout and so you may have an assets folder inside your `androidTest` folder. Your `fixtures` folder must go there.
125125

126126
Because of these differences, there are two implementations of `RequestMatcherRule`: `LocalTestRequestMatcherRule` and `InstrumentedTestRequestMatcherRule`. You should use the generic type for your variable and instantiate it with the required type. Example:
127127

@@ -141,6 +141,33 @@ The difference is that when we run an InstrumentedTest, we must pass the instrum
141141

142142
More on the difference between each kind of test [here](https://medium.com/concrete-solutions/android-local-or-instrumented-tests-9da545af7777#.mmowgemc4)
143143

144+
#### Fixture templating
145+
146+
You can also provide templates for cases when you need dynamic values to be passed to the JSON. For
147+
this to work you need to provide a JSON file with keys that will be replaced by their values in runtime.
148+
149+
A valid JSON template is:
150+
151+
``` json
152+
{
153+
"current_date": "${current_date}"
154+
}
155+
```
156+
157+
And it can be used like this:
158+
159+
``` java
160+
161+
String timestamp = String.valueOf(new Date(778338900L).getTime());
162+
163+
server.addTemplate("json_template.json")
164+
.withValueForKey("current_date", timestamp)
165+
.ifRequestMatches("/get_current_date");
166+
167+
// interact with server and make response assertions
168+
169+
```
170+
144171
## Configuring the `RequestMatcherRule`
145172

146173
It is possible to pass some parameters to the server rule's constructor:

0 commit comments

Comments
 (0)