如何测试CakePHP2.0上的Add函数 [英] How can i test an Add function on CakePHP2.0

查看:98
本文介绍了如何测试CakePHP2.0上的Add函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被告知,我们还必须测试Cake创建的函数,如add / delete ...

I have been told that we have to test also the functions created by Cake like add/delete...

如果我有一个像这样的函数,我可以测试它,如果它没有任何返回,重定向甚至视图? (我使用ajax执行它)

If i have a function like this one, how can i test it if it doesn't have any return, redirect or even a view? ( i use ajax to execute it)

public function add() {
        if ($this->request->is('post')) {
            $this->Comment->create();
            if ($this->Comment->save($this->request->data)) {
                $this->Session->setFlash(__('The comment has been saved'));
            } else {                
                $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
            }
        }
    }

感谢

推荐答案

public function add() {
        $this->autoRender = false;
        if ($this->request->is('post')) {
            $this->Comment->create();
            if ($this->Comment->save($this->request->data)) {
                echo json_encode(array('status' => 'ok'));
            } else {  
                echo json_encode(array('status' => 'fail'));              
            }
        }
    }

这篇关于如何测试CakePHP2.0上的Add函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆