1

Don't find this solution, as more people try testing abstract classes. This is not what i want.

I have patter strategy, and all classes should implements interface. Wanted to add base spec, that rest should extends and add own things.

I should repeat my code in all specs?

Now, I have 2 in to strategies specs:

function it_should_implements_LoadStrategyInterface()
{
    $this->shouldBeAnInstanceOf(LoadStrategyInterface::class);
}

How to not repeat myself?

I try to do AbstractStrategy, but got

Class 'spec\LoadStrategy\Strategies\AbstractStrategy' not found

I'm missing something? Or code like this, is not autoloaded? Or maybe, this is bad practice?

timiTao
  • 1,417
  • 3
  • 20
  • 34

1 Answers1

1

Yes, you should repeat it in every spec. A workaround is to make your Spec file extending another Spec file (where you have this common example) that itself extends the base phpspec class. I would go with explicit describe (testing) this constraint in every concrete class by the way.

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • The after 5 classes with one interface, you would have 5 copies of that. Any test about that interface, you would copy too. And you accept, in any change in interface, you will have to change 5 specs and 5 classes. No have no problem with that? DRY? – timiTao May 13 '17 at 12:50
  • @timiTao you're just testing that an interface is implemented. A think it's not a big deal and, as code is also documentation, in this case I prefer to have this explicit. Btw, as suggested, you can write a superclass and extend from it in every spec file. – DonCallisto May 13 '17 at 13:56