Welcome Guest, Not a member yet? Register   Sign In
testing redirect in controllers
#1

here is the following method inside the controller I am trying to test:
function index(){
if($this->session->userdata('userid')){
$data['username'] = $this->session->userdata('username');
$data['path'] = $this->session->userdata('logo');
$this->load->view('home', $data);

}
else{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}


here is the test method:
public function test_index($userid, $username, $logo_path, $permissions, $tour, $is_admin, $expected){

        //to skip this test uncomment below line
        //$this->markTestSkipped();

        $_SESSION = [
            'userid' => $userid,
            'username' => $username,
            'logo' => $logo_path,
            'permissions' => $permissions,
            'tour' => $tour,
            'is_admin' => $is_admin
        ];

        $output = $this->request('GET', ['Home', 'index']);

        //check if redirect or not
        if($output)
            $this->assertContains($expected, $output);
        else
            $this->assertRedirect('login');
    }


I keep getting the following error: 
Risky Test: Test code or tested code did not (only) close its own output buffers. 
I don't know what am I doing wrong? Can someone please help
Reply
#2

Well, I guess I found a temporary solution. The ci_phpunit-test framework throws an exception and causes phpunit to consider it as a risky test. What I did is I did not throw the exception and saved in the $_SERVER variable that the redirect function was called. I am a newbie and this is my first internship and I believe there are better solutions out there.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB