Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>On how to test if the setting worked see @Gordons answer.</p> <h2>I'd like to argue that you don't <em>need</em> to test that though.</h2> <p>Your unit tests should make sure the <strong>public</strong> API of your class works as expected. You don't care (for the sake of testing) how your values are stored internally so you don't <em>need</em> to make assertions on it. Doing it that way also makes your tests only test <strong>what your class does</strong> and not <strong>how the class does it</strong></p> <p><strong>The point is that you shouldn't have to change your tests when you change your class without affecting what it does</strong></p> <p>For the sake of the argument let's say SomeClass is something that in the end spits out HTML.</p> <pre><code>class SomeClass { public function setValue($name, $value) { // do some stuff } public function doSomething(array $values) { foreach ($values as $name=&gt;$value) { $this-&gt;setValue($name, trim($value)); } } public function createHTML() { $return = ""; foreach($this-&gt;values as $key =&gt; $value) { $return .= "&lt;div is='$key'&gt;$value&lt;/div&gt;"; } return $return; } } </code></pre> <h2>If that is everything your class does an absolutely sufficient test for that class could look like this:</h2> <pre><code>class SomeClassTest extends PHPUnit_Framework_TestCase { public function testHtmlGenerationWithTwoValuesSet() { $o = new SomeClass(); $o-&gt;setValue("foo", "bar"); $o-&gt;setValue("x", "y"); $result = $o-&gt;createHTML(); $this-&gt;assertSame( 2, substr_count("&lt;div&gt;", $result), "There should be as many divs as there are values set" ); $this-&gt;assertTrue( strpos("&lt;div&gt;bar&lt;/div&gt;") !== false "String should contain a set value enclosed in divs" ); } } </code></pre> <p>Again: It is about testing <strong>behavior</strong> of your class, <strong>not testing every method on its own</strong>. Your test suite will be much more valuable if you got about it this way</p> <p>While the example with html might not be the right one it shows how to test behavior pretty nicely (i hope)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload