1

Summary: I want to use Module assertions in my Tests.

Previously I have tested Symfony2 services using PHPUnit tests. This is ok but I would like to use some of the facilities that the Codeception Symfony2 Module provides as well as the cleaner testing style.

I created a new suite with the following services.suite.yml

class_name: ServiceGuy
  modules:
    enabled: [Symfony2, Doctrine2, Filesystem, ServiceHelper]

I ran build and generate:cest and have a ServiceCest.php test file with

public function getServiceUrl(\ServiceGuy $I) {
    $this->myservice = $I->grabServiceFromContainer("myservice");
    $I->wantTo("get service URL");
    $I->seeTrue("http://example.com/services" 
                  ==  $this->ecservice->getServiceUrl());
}

This test passes because I added an assertion function to my ServiceHelper.php file.

class ServiceHelper extends \Codeception\Module    {
  function seeTrue($flag) {
    $this->assertTrue($flag);
  }
}

The Module class has a rich set of assertion functions that I would like to be able to use directly in my tests. But I don't think module object is available to the test. It would seem repetitive to have to add a range of assertion functions to the ServiceHelper. Is there a better way?

For example in the phpunit test I might have these assertions.

$station = $ecservice->getStation("Auckland");

$this->assertEquals(1,count($stations)); $this->assertEquals('Auckland',$station->getDisplayName());

My question is whether there is a way to have all these assertions in the functional test, or whether I have to move a lot of test specific assertions into the helper.

The Unit module appears to provide many of these facilities - but is deprecated.

I tried putting \PHPUnit_Framework_Assert::assertEquals(1,count($stations)); in the test - but this throws an exception on failure that is not handled by the test harness.

Thanks Andrew

PS I'd tag this 'codeception' but I don't have the points yet. perhaps someone else can.

1 Answer 1

2

Yep. We deprecated it because of too much misuses.

In your case you should either use classical PHPUnit's testing style. Codeception have 2 options, generate:test - to get some of Codeception helpers or generate:phpunit to get plain PHPUnit test with no magic.

Please read my post on this. There I have some tips, like working with container in unit tests.

http://codeception.com/02-12-2013/testing-symfony2.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.