如何在Play Framework 2.5上测试表单?

时间:2016-11-02 18:36:05

标签: playframework playframework-2.0

我'我是PlayFramework新手,所以我'阅读它here。但是,该链接适用于旧版本2.1.1

@Test
public void authenticateSuccess() {
    Result result = callAction(
        controllers.routes.ref.Application.authenticate(),
        fakeRequest().withFormUrlEncodedBody(ImmutableMap.of(
            "email", "bob@example.com",
            "password", "secret"))
    );
    assertEquals(302, status(result));
    assertEquals("bob@example.com", session(result).get("email"));
}

上面的代码在当前版本2.5中的外观如何?

1 个答案:

答案 0 :(得分:3)

对于那些可能处于相同情况的人来说,这就是我解决问题的方法:

Rails.application.config.active_record.belongs_to_required_by_default = false

请记住,一旦经过身份验证,我就会将用户重定向到另一个页面,这就是为什么我期待代码@Test public void authenticateAndRedirect() { Result result = route(new Http.RequestBuilder() .method(POST) .bodyForm(ImmutableMap.of("email", "valter@example.com", "password", "secret")) .uri(controllers.routes.ApplicationController.authenticate().url())); assertEquals(SEE_OTHER, result.status()); } @Test public void authenticateAndRedirectJson() throws Exception { JsonNode jsonNode = (new ObjectMapper()).readTree("{ \"email\": \"valter@example.com\", \"password\": \"secret\" }"); Http.RequestBuilder request = new Http.RequestBuilder() .method("POST") .bodyJson(jsonNode) .uri(controllers.routes.ApplicationController.authenticate().url()); Result result = route(request); assertEquals(SEE_OTHER, result.status()); }

SEE_OTHER=303