4 ms·
Ask HN: Unit test makes my mind blow up
Please take a look at the method below. It's a database driven method that tells you if a user id has been mapped to an account id. Nothing special, very straight to the point and it works.
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;
}
}
- uptownhr 12y agoCode doesn't show properly on HN. Here's the http://pastebin.com/y1fhT33G http://pastebin.com/y1fhT33G
- darkmethod 12y agoI think this question may be better suited for http://stackoverflow.com http://stackoverflow.com
- uptownhr 12y agocode with mock usermapp http://pastebin.com/w1HHtwKT http://pastebin.com/w1HHtwKT
- informatimago 12y agoRight. 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...