0

I'm apparently missing something really obvious, but this drives me crazy:

In order to test my code, I need to put my entities into defined states. And it simply doesn't work. It completely ignores any changes I make, like for example this one:

    $this->test_character->setLocation(null);

    $crawler = $this->client->request('GET', '/en/character/start');
    $this->assertTrue($this->client->getResponse()->isSuccessful(), "start page failed to load");
    $this->assertGreaterThan(0, $crawler->filter('html:contains("Character Placement")')->count(), 'start page content failure');

debugging this test shows that it fails because Location is NOT, in fact, set to null. Adding a flush() doesn't change anything, so that's not the issue. My best guess is that it changes it only on the test client, but not on the backend that generates the pages but that leaves the question: How the f*** do I put my entities into a defined state in order to test them ?

1
  • What has $this->test_character anything to do with the test? When you test your backend, you simulate a client (browser) accessing it. You should not do anything different from what a client can do with your application. Commented Feb 18, 2013 at 14:49

1 Answer 1

1

You're on the right lines - the kernel which handles your test request is an isolated instance of your app. It won't be able to access the entity you've presumably loaded into test_character unless you persist and flush it to the database before making the request.

Have you considered using Fixtures? If all you're doing is initialising some entities ready to test a request/response against, that's the correct way to go about it. You should never need to be working directly with entities inside your functional tests.

You may also want to look into Liip functional test bundle to automate setting up your test database and test fixtures.

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

3 Comments

Thanks. Yes, I've considered fixtures, but I kind of disliked the idea of defining the data set in place A and then relying on its specific details in place B - I'd much rather have that bundled in one place. At least that was my thinking behind doing it this way. I'll have a look at the Liip bundle.
I'm going with your solution. It seems to be the way that Symfony wants it done.
Sorry - I downvoted this by mistake. I thought I'd accidentally upvoted it and tried to undo that. It won't let me vote again unless the answer is edited.

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.