1

I'm trying to determine if my approach to testing is ok or completely wrong.

When I looked up how to test database code in Symfony3:

http://symfony.com/doc/current/testing/database.html

They say I should mock stuff. However I'm not doing this.

For example my tests for API - update post:

  • Accessing the container
  • Adding post to test DB
  • Preparing new data to update
  • Hitting API endpoint for update
  • Compare results

I'm not mocking anything. I saw on github that people use it and then I saw that on stack overflow many people don't even mention it.

Since I've written a lot of code I will just point you to specific file:

https://github.com/robertkabat/story/blob/master/tests/CodeCraft/StoryBundle/Controller/Api/StoryApiTest.php

(I know that this code is missing proper assertions but that's not the point)

/** @test */
public function test_logged_user_can_edit_story()
{
    $this->insertFixtures('SingleUser');
    $user = $this->entityManager->getRepository(User::class)->find(1);
    $story = $this->factory(new Story(), [
        'title' => 'Title',
        'teaser' => 'test',
        'content' => 1,
        'user' => 1,
        'status' => 1,
        'view_count' => 1,
        'favourite_count' => 0,
        'created_at' => new \DateTime(),
        'updated_at' => new \DateTime()
    ]);
    $this->entityManager->persist($story);
    $this->entityManager->flush();
    $this->actAsLoggedUser($user);
    $data = [
        'story_form' => [
            'title' => 'Test Title',
            'teaser' => 'Test Teaser - intro to the story that should also by displayed in story index',
            'content' => 'Test Content this is some long, long, long tet content that should be at least 50 
            characters long to pass the test',
        ]
    ];
    $this->client->request('GET', $this->apiEndpoint . '/1', $data);
    $story = $this->entityManager->getRepository(User::class)->find(1);
    $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}

I've created a bunch of helpers and test automation process as u can see in my repo:

  • faster way to load data
  • faster way to act as logged user(this will have JWT version in the future as well)

What I'm trying to learn is how to approach database and functional testing in general.

By looking at provided repository and pointed file(from where you can trace my testing logic), could someone put me on the right track here, or let me know what should I rethink?

Maybe my approach is completely wrong and I should start from scratch?

Thank you in advance

P.S Please remember that's work in progress so a lot of things will not be perfect.

Robert
  • 1,206
  • 1
  • 17
  • 33
  • What's the problem ? Do your tests pass ? – COil May 21 '17 at 08:53
  • @COil Yep, tests are giving me expected results. There's no problem in general with tests. It's just different then docs and different then test that I saw in various applications. I'm just wondering if it's good practice, does it matter how I do tests, should I mock objects instead just using them as I do now. – Robert May 21 '17 at 11:47
  • 1
    What you are currently doing is called integration testing as you are testing the whole application. This is not wrong in any way, it is your decision how you want to test your application. Unit tests vs integration tests: http://stackoverflow.com/questions/5357601/whats-the-difference-between-unit-tests-and-integration-tests. Choose the way to test (or multiple ways) which suits best in your specific scenario (and I personally think integration tests are a good decision) – Florian Moser May 21 '17 at 12:55
  • @FlorianMoser Thank you very much for comment, it's very helpful. Of course I was aware of the difference between tests. I just wanted the tests to fall in line with Symfony's good practices. But if you say it's ok to just choose whatever I need I will take that as an answer to my question. You can publish answer like that and I will accept it so other people will have easier job finding it. – Robert May 21 '17 at 13:11
  • I'd rather not publish this as an answer, as it is primarily my personal opinion. There may be someone who can show an example of better testing, or you can share some insights when you've finished your testing. – Florian Moser May 21 '17 at 13:20
  • Fair enough. I will try to prepare small summary when I will finish that part of the testing and publish it somewhere. Anyway not I at least know it's not horrible. – Robert May 21 '17 at 13:22
  • Ask yourself what it is your are actually testing. Most of your code seems to deal with the entity manager and there really is not much point in test it. – Cerad May 21 '17 at 15:02

0 Answers0