2011-04-21 27 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')); 
相关问题