Given.the(author).was_able_to(write_a_simple_narrative_test_in(codeDir));
...[snip]...
private Action<ScriptWriter, AuthorActor> write_a_simple_narrative_test_in(final File codeDir) {
return new Action<ScriptWriter, AuthorActor>() {
@Override
public void performFor(AuthorActor actor) {
actor.writeSomeCodeIn(codeDir);
}
};
}
...[snip]...
public void writeSomeCodeIn(File codeDir) {
this.codeDirectory = codeDir;
try {
FileUtils.copyFileToDirectory(new File("scriptwriter2/src/test/fixtures/BasicArithmeticTest.java"), codeDir);
} catch (IOException e) {
e.printStackTrace();
}
}
I'd like to replace the file with inline text so the test is self-contained. Then the test would read
private static final String A_SIMPLE_NARRATIVE_TEST =
"package com.youdevise.narrative.example;
+ "import com.youdevise.test.narrative.*;" + ...[snip]...
Given.the(author).was_able_to(write(A_SIMPLE_NARRATIVE_TEST));
...[snip]...
private Action<ScriptWriter, AuthorActor> write(final String javaCode) {
return new Action<ScriptWriter, AuthorActor>() {
@Override
public void performFor(AuthorActor actor) {
actor.write(javaCode);
}
};
}
...[snip]...
public void write(String javaCode) {
// Write the code to a temporary file on the filesystem; if needed, use actor memory to store the filename
}
and ScriptWriter could render the test as
Given the author was able to write [a simple narrative test]
where the bit in brackets is a link that opens a popup or new page with the text used.
Any thoughts on this? I can't seem to find any provision for heredocs or variables or similar in Cucumber to solve similar problems of lengthy text in tests, though Aslak does suggest you avoid Rails "fixtures" for a similar reason (keeping your tests self-contained): https://github.com/aslakhellesoy/cucumber/wiki/Given-When-Then
This kind of tension between making something inline vs. putting it in a file is a common tension that I find in langs that don't support heredocs or data sections.
Andy