

Ask HN: Unit test makes my mind blow up - uptownhr

Please take a look at the method below. It&#x27;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.<p>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&#x2F;end to end test? Someone please explain and expand on this.<p>Thank you<p><i>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?<p>class User{
	var $db;<p>function __construct(){
		$this-&gt;db = new Some_DB();
	}<p>&#x2F;</i>
	* returns true if user user owns account
	*&#x2F;
	function is_user_account($user_id, $acct_id){
		$db = $this-&gt;db;<p><pre><code>		$sql = &quot;SELECT um_active FROM UserMaps WHERE acct_id = ? AND user_id = ?&quot;;
		$res = $db-&gt;DbGetOne($sql, array($acct_id,$user_id));

		return $res;
	}
</code></pre>
}<p>function test_is_user_account(){
        $user = new User();
		$user-&gt;db = new MockDB();<p><pre><code>		$has_access = $user-&gt;is_user_account(1,1);
		assertTrue($has_access);
</code></pre>
}<p>class MockDB{<p>function DbGetOne($sql, $params){
		return 1;
	}
}
======
informatimago
Right.

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
Code doesn't show properly on HN. Here's the
[http://pastebin.com/y1fhT33G](http://pastebin.com/y1fhT33G)

------
darkmethod
I think this question may be better suited for
[http://stackoverflow.com](http://stackoverflow.com)

------
uptownhr
code with mock usermapp
[http://pastebin.com/w1HHtwKT](http://pastebin.com/w1HHtwKT)

