Skip to content

Commit

Permalink
[playframework#954] Enhance FunctionalTest to give access to renderArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
revbingo committed Jul 2, 2011
1 parent 4779bb8 commit 72acb5f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion documentation/manual/test.textile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class MyTest extends UnitTest {

h3. Functional test

A functional test is written using JUnit. In this kind of test you can test your application by accessing directly the controller objects.
A functional test is written using JUnit. In this kind of test you can test your application by accessing directly the controller objects.

Here is an example of a Functional test:

Expand All @@ -51,6 +51,16 @@ public class ApplicationTest extends FunctionalTest {

}

Using the @renderArgs()@ method, you can also get direct access to the arguments passed to the view, instead of having to make assertions about the response itself. For example:

bc. @Test
public void testUserIsFoundAndPassedToView() {
Response response = POST("/user/find?name=mark&dob=18011977")
assertThat(renderArgs("user"), is(notNullValue());
User user = (User) renderArgs("user");
assertThat(user.name, is("mark"));
}

h3. Selenium test

Acceptance tests are written using Selenium. Here you can test your application by running it in an automated browser.
Expand Down
14 changes: 13 additions & 1 deletion framework/src/play/test/FunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import play.mvc.Http;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.Scope.RenderArgs;

import com.ning.http.multipart.FilePart;
import com.ning.http.multipart.MultipartRequestEntity;
Expand All @@ -44,6 +45,8 @@ public abstract class FunctionalTest extends BaseTest {

private static Map<String, Http.Cookie> savedCookies; // cookies stored between calls

private static Map<String, Object> renderArgs = new HashMap<String, Object>();

@Before
public void clearCookies(){
savedCookies = null;
Expand Down Expand Up @@ -261,8 +264,13 @@ public static void makeRequest(final Request request, final Response response) {
final Future invocationResult = TestEngine.functionalTestsExecutor.submit(new Invoker.Invocation() {

@Override
public void execute() throws Exception {
public void execute() throws Exception {
renderArgs.clear();
ActionInvoker.invoke(request, response);

if(RenderArgs.current().data != null) {
renderArgs.putAll(RenderArgs.current().data);
}
}

@Override
Expand Down Expand Up @@ -428,6 +436,10 @@ public static String getContent(Response response) {
throw new RuntimeException(ex);
}
}

public static Object renderArgs(String name) {
return renderArgs.get(name);
}

// Utils

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,13 @@ public void usingRedirection() {
assertIsOk(response);
}

@Test
public void canGetRenderArgs() {
Response response = GET("/users/edit");
assertIsOk(response);
assertNotNull(renderArgs("u"));
User u = (User) renderArgs("u");
assertEquals("Guillaume", u.name);
}
}

0 comments on commit 72acb5f

Please sign in to comment.