rest - Is this an ideal way of testing code that inserts data to a table? -


I am working on implementing a REST API using Symfony2. I'm trying to follow a Test Driven Development (TDD) approach I am quite new to both Symfony2 and TDD.

I have written this exam:

  public function test.post () {// i) Client = permanent request: createClient (); $ Request = $ client- & gt; Request ('POST', '/ api / outlets', array (), array (), array ('CONTENT_TYPE' => 'application / json'), '{"name": "test outlet"}') ; // ii) Test the reaction- $ - gt; AssertJsonResponse ($ client-> getResponse (), 201, incorrect); }  

This administrator checks for action:

  public function postAction (request $ request) {$ entity = new outlet (); $ Form = $ this- & gt; CreateForm (new outlet type), $ unit, array ("method" = & gt; $ request-> getMethod ()); $ This- & gt; Remove ExtraFields ($ request, $ form); $ Form & gt; HandleRequest ($ request); If ($ form-> isValid ()) {$ em = $ this- & gt; GetDoctrine () - & gt; GetManager (); $ Em- & gt; Continues ($ unit); $ Em- & gt; Flush (); Return unit; } Return FOSView :: Create (array ('Errors' = & gt; $ form-> getErrors ()), code :: HTTP_INTERNAL_SERVER_ERROR;}  

Everything works as expected .

Every time the test is run for the above action, it creates a new outlet in the database Is this ideal? Or can not I release the data to the database in the test?

There are several ways to handle such tests that are firmly embedded in the database They are sometimes you want to test a form functionally by filling out the data, then submit the form, then check the status code and finally validate the content. Sometimes you need a set of fixtures already

First of all, you need to use a test database in your test environment.

In app / config_test.yml: - database using env = test Options.

The easiest way to keep your database clean is to clean your test database before each test.

In your tests:

  Use Principles \ Common \ DataFixtures \ purger \ ORmpurger; ... Protected function setup () {$ this- & gt; Client = Fixed :: createClient (array ('environment' =>; 'test')); $ Net = new ORMPurger ($ this-> client- & gt; getContainer () - & gt; get ('doctrine.orm.entity_manager')); $ Purger- & gt; SetPurgeMode (ORMPurger :: PURGE_MODE_DELETE); $ Purger- & gt; Purge (); }  

If you purge it into tyride () , then a test can potentially crash and the following tests will not have a clean database.

You need "theory / data-fixture" in your dependency.

Of course there are many other solutions.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -