2011-04-21 26 views
1

我想爲接收帶JSON格式數據的POST方法的操作創建功能測試。功能測試:如何在POST方法中設置JSON數據?

這是我有:

info('set car')-> 
    post('/user/'.$user->getId().'/set-car/'.$car->getId()'-> 

    with('request')->ifFormat('json')->begin()-> 
    isParameter('module', 'myModule')-> 
    isParameter('action', 'myAction')-> 
    end()-> 

But..where我應該設置接收JSON數據?

SF 1.4

問候

哈維

回答

2

你有沒有簽出從Jobeet的教程page

第一個例子是

$browser = new sfBrowser(); 

$browser-> 
    get('/')-> 
    click('Design')-> 
    get('/category/programming?page=2')-> 
    get('/category/programming', array('page' => 2))-> 
    post('search', array('keywords' => 'php')) 
; 

這可能是因爲我不明白你質詢。 糾正我,如果我錯了。

+0

你測試你想要什麼謝謝,但在行動中,我通過$ request-> getContent()檢索JSON日期。所以,如果我在你粘貼的post()函數中寫入JSON數據,我在運行測試時從$ request-> getContent()調用中獲得'NULL'(當然,如果我使用普通瀏覽器FF,$ request-> getContent()可以正確返回JSON數據)。那麼,你有什麼建議? – ziiweb 2011-04-28 10:24:31

+0

以及我的意思是「JSON數據」,而不是「JSON日期」。 – ziiweb 2011-04-28 10:30:42

+0

你嘗試了$ request-> getParameter('data');或$ request-> getPostParameter('data')。顯然,你必須有數據= {我們的JSON在這裏}。它應該工作。 – maectpo 2011-04-28 15:04:53

0

更多的一種解決方法......

如果你得到像這樣在你的行動內容:

protected function getContent() { 
    $content = $this->request->getParameter('content'); 
    if(!$content) $content = $this->request->getContent(); 
    return $content; 
} 

public function executeIndex(sfWebRequest $request) { 
    $content = $this->getContent(); 
    //do something with your content :D 
} 

應該允許通過傳遞

post('search', array('content' => 'whatever my content is')); 
相關問題