![]() 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. Say that you have a class that sends an email to a user whenever an order is placed. So, what is a mock A mock is a 'fake version' of an object. It basically comes down to the fact that you pick a specific class and replace that with a so-called Mock. The most common are:Īssertion methods come from PHPUnit. First of all, what is mocking Mocking means that you 'fake' a class. There are pretty many assertions you can use inside tests. 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |