Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests should not do any network connections at all. #52

Open
smentek opened this issue Jun 17, 2015 · 0 comments
Open

Unit tests should not do any network connections at all. #52

smentek opened this issue Jun 17, 2015 · 0 comments

Comments

@smentek
Copy link

smentek commented Jun 17, 2015

The way to go with unit tests is:

  • Refactor in the way to encapsulated all the code about interaction with remote server (curl, etc) in objects so the only responsibility of this objects will be those connections and nothing else. (this is about Emitters). At the end of refactoring there is no one variable or method with "debug" in name.
  • Rewrite the tests in the way that tests uses mocks (https://phpunit.de/manual/current/en/test-doubles.html) of those objects instead of real objects. Since objects are only about remote connections there is no need for us to test it since curl library has its own tests in PHP core. In test if right methods are called on mocks the right quantity of time and with proper values of parameters.

$emitterCurlMock = //Here lots of lines that would set up mock!
// use: disableOriginalConstructor()
// set how what methods mock have and how many times they should be called
// Use returnCallback() to return somthing so code outside would "think" it deals with real object
// So at this point we know that right mock methods has been called right amount of time

$subject = Snowplow\Tracker\Subject()
$trucker = new Tracker($emitterCurlMock, $subject, "nodejs-tracker")

//Here we run our processing:
$trucker->trackUnstructEvent($json, $context, $timestamp)

//Here we get out from our tracker what data would be send (is not send since we use mock).
// So for instance if we send some JSON in a body check if this json is right:
$this->assertEquals($trucker->useApiToGetJsonThatWouldBeSend(), "{this is the json}");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant