Answer the question
In order to leave comments, you need to log in
How to properly cover code with tests?
I am developing my own smsBundle and decided to learn how to write tests correctly. Questions arose during the process. Actually, here is one of my test examples - https://github.com/manyrus/SmsBundle/blob/ba6a45cd... for the class https://github.com/manyrus/SmsBundle/blob/ba6a45cd... . Is it too much code? And that monstrosity scares me. And what about code duplication? One point in particular worries me. Is it worth it to do this check with a callback:
$obj = $this;//php 5.3 support
$eventDispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcher');
$sms = $this->getSmsMessage();
$eventDispatcher
->expects($this->at(0))
->method('dispatch')
->with($this->equalTo(MergeEvents::ON_MERGE)
, $this->isInstanceOf('Manyrus\SmsBundle\Lib\Event\MergeEvent'))
->will($this->returnCallback(
function($name,MergeEvent $mergeEvent) use($sms, $obj){
$obj->assertEquals($mergeEvent->getMessage(), $sms);
}
));
//где-нибудь в другом классе, куда подставили $eventDispatcher
public function send(SmsMessage $sms) {
//some action
$event = new MergeEvent();
$event->setSmsMessage($sms);
$this->dispatcher->dispatch(MergeEvents::ON_MERGE, $event);
}
Answer the question
In order to leave comments, you need to log in
Duplicate code in tests should be eliminated. Read Kent Beck's XP Programming, he touched on the issue of test refactoring, how often it should be done, etc.
Regarding the monstrosity, but what can you do if you need to mock services? In fact, if you remove the duplication and put everything into the helper methods, then it’s not so scary.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question