Home > Mobile >  Is a (unit) test that mocks the result of a method call really valid?
Is a (unit) test that mocks the result of a method call really valid?

Time:02-16

I created a test method for a controller, but I am not sure if the test is really valid.
Inside the controller there's a method call that returns an object which one of the properties is a XML string. I want to test if that XML contains a specific tag and so I created a text file with that xml and mocked the method call setting as result the xml read from the text file then I assert that the wanted tag is there.
So my question is, if the real method call changes and stop returning the correct XML with the tags I want, will my test fail or keep passing?

CodePudding user response:

Your test will keep passing of course. But the point of unit tests is testing one UNIT of code. In your case you are testing the controller action. Particularly you are testing that:

//Prepare
When my inner method returns some valid XML that I expect and the correct tag is present there.
//Act
Call the method
//Assert
Assert that my controller action will return status 200 OK (or similar)

You are not testing the logic inside the method you are calling. Such test is called integration test. In those test you are testing how 2 components integrate with each other.

  • Related