2016-11-02 11 views
2

私はPlayFrameworkを初めて使ったので、それについてはhereと読んでいます。上記のコードは、現在のバージョン2.5で次のようになりますどのようにPlay Framework 2.5でフォームをテストする方法は?

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

:ただし、リンクは古いバージョン、2.1.1のためにあるのですか?同じような状況にあるかもしれない人のために

答えて

2

は、ここで私は私の問題を解決する方法は次のとおりです。

@Test 
public void authenticateAndRedirect() { 
    Result result = route(new Http.RequestBuilder() 
      .method(POST) 
      .bodyForm(ImmutableMap.of("email", "[email protected]", "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\": \"[email protected]\", \"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()); 
} 

のことを、認証されたsの私は「彼いったん別のページにユーザーをリダイレクトするM」があることに注意してくださいなぜ私はコードを期待しているのですか?SEE_OTHER=303

public Result authenticate() { 
    Form<Login> form = formFactory.form(Login.class).bindFromRequest(); 

    if (form.hasErrors()) { 
     return badRequest(login.render(form)); 
    } 

    Optional<User> maybeUser = User.findByEmail(form.get().email); 

    if (maybeUser.isPresent()) { 
     session("email", maybeUser.get().email); 
     return redirect(routes.ApplicationController.index()); 
    } 

    return badRequest(login.render(form)); 
} 
関連する問題