![]() Stubs can also be created using static methods from Codeception\Stub class. Inside unit tests ( Codeception\Test\Unit) it is recommended to use alternative API: make ( UserRepository :: class, ) $userRepository -> find ( 1 ) // => User // create a dummy $userRepository = $this -> makeEmpty ( UserRepository :: class ) // create a stub with all methods replaced except one $user = $this -> makeEmptyExcept ( User :: class, 'validate' ) $user -> validate ( $data ) // create a stub by calling constructor and replacing a method $user = $this -> construct ( User :: class, , ) // create a stub by calling constructor with empty methods $user = $this -> constructEmpty ( User :: class, ) // create a stub by calling constructor with empty methods $user = $this -> constructEmptyExcept ( User :: class, 'getName', ) $user -> getName () // => davert $user -> setName ( 'jane' ) // => this method is empty 'john' ]) $name = $user -> getName () // 'john' Stubs can be created with a static methods of Codeception\Stub. Under the hood it used PHPUnit’s mock builder but with much simplified API.Īlternatively, Mockery can be used inside Codeception. Test DoublesĬodeception provides Codeception\Stub library for building mocks and stubs for tests. ![]() The most common are:Īssertion methods come from PHPUnit. The parameters of MOCKMETHOD mirror the method declaration. There are pretty many assertions you can use inside tests. Defines a mock method methodname with arguments ( args.) and return type returntype within a mock class. Or simply run the whole set of unit tests with: php vendor/bin/codecept run unitĪ test created by the generate:test command will look like this: setName ( null ) $this -> assertFalse ( $user -> validate ()) $user -> setName ( 'toolooooongnaaaaaaameeee' ) $this -> assertFalse ( $user -> validate ()) $user -> setName ( 'davert' ) $this -> assertTrue ( $user -> validate ()) } } Assertions It creates a new ExampleTest file located in the tests/unit directory.Īs always, you can run the newly created test with this command: php vendor/bin/codecept run unit ExampleTest Creating a TestĬreate a test using generate:test command with a suite and test names as parameters: php vendor/bin/codecept generate:test unit Example ![]() If you ever wrote a PHPUnit test then do it just as you did before.Ĭodeception adds some nice helpers to simplify common tasks. Thus, any PHPUnit test can be added to a Codeception test suiteĪnd then executed. Current version is 5.x Read for latest version Unit
0 Comments
Leave a Reply. |