如何用CodeIgniter测试控制器? [英] How to test controllers with CodeIgniter?

查看:119
本文介绍了如何用CodeIgniter测试控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用CodeIgniter MVC框架构建的PHP Web应用程序。我想测试各种控制器类。我使用 Toast 进行单元测试。我的控制器没有状态,他们处理的一切都保存到会话或传递到查看显示。创建一个模拟会话对象并测试它是否能正常工作是简单的(只需创建一个mock对象并注入$ controller-> session = $ mock)。



不知道,是如何使用视图。在CodeIgniter中,视图被加载为:

  $ this-> load-> view($ view_name,$ vars,$返回); 

由于我不想更改CI代码,我虽然可以创建一个模拟Loader原本的。这里面的问题,我找不到一种方法从CI_Loader派生一个新类。



如果我不包括系统/ libraries / Loader.php文件,类CI_Loader是未定义的,我不能继承它:

  class Loader_mock extends CI_Loader 

如果我包含文件(使用require_once),我得到错误:

 无法重新声明类CI_Loader 

看起来像CI代码本身不会因为任何原因使用require_once。



编辑:我试图在运行时注入一个真正的加载器对象一个mock类,并使用__call,__set,__get,__isset和__unset重定向所有调用和变量。但是,它似乎不工作(我没有得到任何错误,但没有输出,即从Toast的空白页)。这里是代码:

  class Loader_mock 
{
public $ real_loader;
public $ varijable = array();

public function Loader_mock($ real)
{
$ this-> real_loader = $ real;
}

public function __call($ name,$ arguments)
{
return $ this-> real_loader-> $ name
}

public function __set($ name,$ value)
{
return $ this-> real_loader-> $ name = $ value;
}

public function __isset($ name)
{
return isset($ this-> real_loader-> $ name);
}

public function __unset($ name)
{
unset($ this-> loader-> $ name);
}

public function __get($ name)
{
return $ this-> real_loader-> $ name;
}

公共函数视图($ view,$ vars = array(),$ return = FALSE)
{
$ varijable = $ vars;
}
}


解决方案

,您可以这样做:

  $ CI =& get_instance(); 
$ CI = load_class('Loader');

class MockLoader extends CI_Loader
{
function __construct()
{
parent :: __ construct();然后在你的控制器中做$ this-> load =













$ b $ new MockLoader()。


I have a PHP web application built with CodeIgniter MVC framework. I wish to test various controller classes. I'm using Toast for unit testing. My controllers have no state, everything they process is either saved into session or passed to view to display. Creating a mock session object and testing whether that works properly is straightforward (just create a mock object and inject it with $controller->session = $mock).

What I don't know, is how to work with views. In CodeIgniter, views are loaded as:

$this->load->view($view_name, $vars, $return);

Since I don't want to alter CI code, I though I could create a mock Loader and replace the original. And here lies the problem, I cannot find a way to derive a new class from CI_Loader.

If I don't include the system/libraries/Loader.php file, the class CI_Loader is undefined and I cannot inherit from it:

class Loader_mock extends CI_Loader

If I do include the file (using require_once), I get the error:

Cannot redeclare class CI_Loader

Looks like CI code itself does not use require_once from whatever reason.

Does anyone here have experience with unit testing CodeIgniter powered applications?

Edit: I tried to inject a real loader object at run-time into a mock class, and redirect all calls and variables with __call, __set, __get, __isset and __unset. But, it does not seem to work (I don't get any errors though, just no output, i.e. blank page from Toast). Here's the code:

class Loader_mock 
{
    public $real_loader;
    public $varijable = array();

    public function Loader_mock($real)
    {
        $this->real_loader = $real;
    }

    public function __call($name, $arguments) 
    {
        return $this->real_loader->$name($arguments);
    }

    public function __set($name, $value)
    {
        return $this->real_loader->$name = $value;
    }

    public function __isset($name)
    {
        return isset($this->real_loader->$name);
    }

    public function __unset($name)
    {
        unset($this->loader->$name);
    }

    public function __get($name)
    {
        return $this->real_loader->$name;
    }

    public function view($view, $vars = array(), $return = FALSE)
    {
        $varijable = $vars;
    }
}

解决方案

Alternatively, you could do this:

$CI =& get_instance();
$CI = load_class('Loader');

class MockLoader extends CI_Loader
{
    function __construct()
    {
        parent::__construct();
    }
}

Then in your controller do $this->load = new MockLoader().

这篇关于如何用CodeIgniter测试控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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