Permalink
Browse files

Merge pull request #281 from revbingo/lighthouse-954-patch

[#954] Enhance FunctionalTest to give access to renderArgs
  • Loading branch information...
pepite committed Aug 8, 2011
2 parents d708ac0 + 72acb5f commit 22790323334a739ec7244e5c650208794658daaf
@@ -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:
@@ -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.
@@ -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;
@@ -44,6 +45,8 @@
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;
@@ -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
@@ -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
@@ -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 2279032

Please sign in to comment.