jeudi 18 février 2016

How should I unit test resource dependent methods (URL)?

The question may sound simple rather for you, but I've been looking for an answer and haven't found anything strightforward.

How can I unittest methods like this (pseudocode):

def fetchAndCreate(url: String){
   val response = request.get(url) //HTTP get request
   val data = HTML.parse(response) //parse html into an object, let's say it has a .name field
   return data.name
}

For an invalid url it's rather simple. Provide a random string and check for the exception. But how am I supposed to write a test, to check the result for valid url. Of course I could hardcode "valid" URL and its expected result, then call my method inside unit test (with "valid" url) and compare results. But what if the url becomes invalid after some time? How am I supposed to test it?

Aucun commentaire:

Enregistrer un commentaire