我試圖用simpletest測試我的codeigniter系統,但是我不斷收到錯誤。我對PHP和codeigniter非常新,所以請放心。Codeigniter測試 - 調用非對象的成員函數
Fatal error: Call to a member function add() on a non-object in C:\xampp\htdocs\di2\application\tests\simple_test.php on line 35
這是我的測試文件,此刻我試圖簡單地向數據庫添加一個新用戶。
require_once BASEPATH."core/Model.php";
require_once APPPATH."models/Users_model.php";
class ExampleTest extends UnitTestCase
{
function ExampleTest()
{
parent::__construct('Example tests');
}
///// All functions starting with "test" will be tested /////
function testSimpleStuff()
{
$name = 'Andreas';
$online = TRUE;
$this->assertEqual($name, 'Andreas');
$this->assertTrue($online);
}
//There are already 8 members in the table
function testAdd()
{
$this->model->add("new member");
$this->assertEqual($this->model->get(9),'new member');
$this->assertNull($this->model->get(0));
$this->assertNull($this->model->get(10));
}
}
這是我打電話的模式:
class Users_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
//Used to retrieve all of the members from the database and list them by their id in descending order
function get($id = null)
{
$this->db->select('id, first_name, last_name, dob, email_address, phone_number, address_line_1, address_line_2, town, county, postcode, student, volunteer');
$this->db->from('Users');
if (!is_null($id)) $this->db->where('id', $id);
$this->db->order_by('id', 'desc');
return $this->db->get()->result();
}
function add($data)
{
$this->db->insert('Users', $data);
return $this->db->insert_id();
}
function update($data, $id)
{
$this->db->where('id', $id);
$this->db->update('Users', $data);
return $this->db->affected_rows();
}
function delete($id)
{
$this->db->where('id', $id);
$this->db->delete('Users');
return $this->db->affected_rows();
}
function filterUsers()
{
return $this->db->get('Users')->result();
}
}
誰能告訴我爲什麼我得到這個錯誤,我怎麼能去修復它。提前歡呼。
我不習慣笨,但你創建的類Users_Model的對象?似乎'$ this-> model'不是一個對象... – briosheje