testing return type of getters?

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

testing return type of getters?

Post by koen.h »

Is it meaningful to test the return type of getters? eg test if a method returns a string, array, ...?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: testing return type of getters?

Post by Christopher »

I don't think it would hurt. But you have to maintain the test. And I think it depends on the getter/setter. If it is supposed to convert/enforce all data to a specific type then it would make sense to test the type. Especially if there is a good chance that the problem could occur. Otherwise you are on the line between unit tests and smoke tests.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: testing return type of getters?

Post by josh »

Use assertIdentical, if you find the test is too brittle relax it with assertEquals
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: testing return type of getters?

Post by Jenk »

No, there's no point. Your asserts should not be that explicit, but should be implicit enough to cover such concerns. Testing types is enforcing tight-coupling. Bad.

Code: Select all

public function testGetter () {
  $foo = $objectUnderTest->getSomething();
  $this->assertTrue($foo->fooBar());
}
The above is implicitly testing that what ever is returned by getSomething() implements fooBar().
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: testing return type of getters?

Post by koen.h »

Why not make the implicit explicit? If one needs something implicitly tested, isn't that a sign it should be tested explicitly or not?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: testing return type of getters?

Post by Jenk »

No, because what happens when you need to use the component with a different type, but implements the same interface? You'll get a failure.

The entire point of OO is to be implicit. If it walks and talks like a duck, it's a duck. I don't need to match it's DNA to check it's a duck.
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: testing return type of getters?

Post by koen.h »

Ideally the return type would be part of the interface but that's a feature not yet available. If it walks and talks like a duck but lays duck-eggs one time, but chicken-eggs the other time it will break the system.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: testing return type of getters?

Post by Jenk »

and when you try and treat what was just laid as a duck egg, your test will implicitly fail.
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: testing return type of getters?

Post by koen.h »

And then I'd have to find out what the problem is while with an explicit test it's known immediately.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: testing return type of getters?

Post by Benjamin »

I'd probably only test for that if it needs to be an explicit type. If something needs to return an object I would test for that, maybe even test to verify it's an instance of a particular class.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: testing return type of getters?

Post by Chris Corbyn »

Yeah I disagree with Jenk here. I don't think you should test return types all the time, but I don't think you should never test a return type. That's almost like saying the === operator has no place in PHP.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: testing return type of getters?

Post by Jenk »

Not quite.. === isn't comparing type, it's only comparing instance.

Code: Select all

$this->assertIdentical($registry->get('foo'), $registry->get('foo'));
That's a pass..

If you are required to have an explicit type returned, then ok.. but I really cannot think of *any* scenario where this is applicable, save for maybe one which is a misnomer.. a test for a Factory. At the *most* I'd agree on testing for an interface (rather than concrete class); but then I disagree with type stricting at all. Interfaces and Notations are the kick-back from using type-strict in the first place, rather than ditching type restrictions you have to work with such inferior work arounds :P
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: testing return type of getters?

Post by Jenk »

I'm actually interested in hearing some examples of where explicitly testing the return type would be required, if anyone has an example. :)
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: testing return type of getters?

Post by koen.h »

An example would be (from memory) Zend Authentication. It returns an Result object. Something like this:

Code: Select all

 
<?php
$result = $auth->login($username, $pass);
if ($result->isAuthenticated()) {}
?>
 
Here I would like to be sure the returned result-object implements a certain interface.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: testing return type of getters?

Post by Jenk »

Which is implicitly tested by calling isAuthenticated(). I wouldn't test for the interface then.
Post Reply