2017-12-30 54 views
0

내 yesod 테스트에서 나는 테스트 중간에 db의 레코드를 수정할 수 있기를 원합니다. 여기 yesod 테스트에서 DB 작업 실행

이 오류와 함께 실패합니다 내가

 yit "post is created by authorized user" $ do 
      request $ do 
       addPostParam "ident" "dummy" 
       setMethod "POST" 
       setUrl ("http://localhost:3000/auth/page/dummy" :: Text) 
      update 0 [UserAuthorized =. True] 
      postBody PostR (encode $ object [ 
       "body" .= ("test post" :: Text), 
       "title" .= ("test post" :: Text), 
       "coverImage" .= ("test post" :: Text), 
       "author" .= (0 :: Int) 
       ]) 
      statusIs 200 

와 함께 제공되는 코드

• Couldn't match expected type ‘IO a0’ 
       with actual type ‘ReaderT backend0 m0()’ 
• In the second argument of ‘($)’, namely 
    ‘update 0 [UserAuthorized =. True]’ 

    In a stmt of a 'do' block: 
    runIO $ update 0 [UserAuthorized =. True] 
    In the expression: 
    do { settings <- runIO 
        $ loadYamlSettings 
         ["config/test-settings.yml", "config/settings.yml"] [] useEnv; 
     foundation <- runIO $ makeFoundation settings; 
     yesodSpec foundation $ do { ydescribe "Auth" $ do { ... } }; 
     runIO $ update 0 [UserAuthorized =. True]; 
     .... } 

내가 update 반환 m() 대신 request 같은 YesodExample site()postBodystatusIs 않기 때문이다 말할 수에게 있습니다 .

이 테스트에서 어떻게 데이터베이스 업데이트를 수행 할 수 있습니까?

답변

0

, SIBI로 첫 번째 지적은 내가 두 번째는 방금 정수로 기록을 찾아 볼 수 없다 runDB이 필요하다.

이 작업을 얻으려면 다음 코드

runDB $ do 
    (first :: Maybe (Entity User)) <- selectFirst [] [] 
    case first of 
     Nothing -> pure() -- handle the case when the table is empty 
     Just (Entity k _) -> update k [UserAuthorized =. True] 

이 발견은 DB에서 레코드의 사용 후를 업데이트합니다. (first :: Maybe (Entity User)) <- selectFirst [] []을 수정하여 업데이트 할 레코드를 선택하십시오.

2

runDB 기능을 사용해야합니다. 즉 :

이 2 문제가 있었다
runDB $ update 0 [UserAuthorized =. True] 
+0

이 중 절반은 해결되었으므로 앞으로 곧 게시 할 다른 문제는 거의 끝났습니다. – Qwertie