Ask HN: Unit test makes my mind blow up
1 points| uptownhr | 11 years ago
So my main question, is it really worth writing a unit test for a method like this? Or, is a unit test pretty much useless for a method like? Should time be spent on integration/end to end test? Someone please explain and expand on this.
Thank you
fyi: I know you can throw in a mock $user_map array in there to test for various user id and account id combinations. Is this really the way to go and write?
class User{ var $db;
function __construct(){ $this->db = new Some_DB(); }
/ * returns true if user user owns account */ function is_user_account($user_id, $acct_id){ $db = $this->db;
$sql = "SELECT um_active FROM UserMaps WHERE acct_id = ? AND user_id = ?";
$res = $db->DbGetOne($sql, array($acct_id,$user_id));
return $res;
}
}function test_is_user_account(){ $user = new User(); $user->db = new MockDB();
$has_access = $user->is_user_account(1,1);
assertTrue($has_access);
}class MockDB{
function DbGetOne($sql, $params){ return 1; } }
[+] [-] informatimago|11 years ago|reply
It looks like this only tests whether MocDB.DbGetOne() returns true, which it always does, and which is not testing the program, but the test.
You have to test the tests, to be sure they'll correctly test the program. Of course, then you also need to test the tests that tests the tests, and so on...
[+] [-] uptownhr|11 years ago|reply
[+] [-] darkmethod|11 years ago|reply
[+] [-] uptownhr|11 years ago|reply