Discussion

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; } }

1 points uptownhr 12 years ago 4 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton