문제

Exist some class:

class A{
  protected $options;
  public function setDefaults($defaults){
      $this->options=$defaults;
  }
  public function getOptions(){
     return  $this->options;
  }
}

And other class:

class B{
public function setDefaultOptions(A $options){
   $options->setDefaults('foo'=>'bar')
}

}

Uses:

$a=new A();
$b=new B();
$b->setDefaultsOptions(a);

How I can check in spec for B that $a->options['foo'] is set?


Now I implement it as:

  public function getMatchers(){
    return array('haveResolverKey'=>
    function($subject, $key){
        $resolver=new A();
        $subject->setDefaultOptions($resolver);
        $ret=$resolver->getOptions();

        return isset($ret[$key]);
    });
  }

Maybe exist easy way?

도움이 되었습니까?

해결책

How I can check in spec for B that $a->options['foo'] is set?

You shouldn't. Verify the behaviour instead of a state.

In other words you should verify outputs of methods and express expectations against collaborators, instead of verifying the internal state of your classes.

Internals might change once you refactor, behaviour should remain the same.

As far as I understood your example, it could be described as:

class BSpec extends ObjectBehavior
{
    function it_sets_defaults_on_a(A $a)
    {
        $a->setDefaults(array('foo' => 'bar'))->shouldBeCalled();

        $this->setDefaultOptions($a);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top