简体   繁体   中英

How can mock object replace all system functionality being tested?

I am fairly new to unit testing. And at the moment I have trouble on trying to unit test a Google oAuth Picasa authentication. It involves major changes to the code if I would like to unit tested it (yeah, I develop unit test after the app works).

I have read that Mock Object is probably the way to go. But if I use Mock, how do I know that the functionality (that is Google oAuth Picasa authentication), is really working?

Or, aside that I develop unit testing after the app finished, did I made other mistakes in understanding Mock?

When unit testing, you test a particular unit (function/method...) in isolation , meaning that you don't care if other components that your function uses, work (since there are other unit test cases that cover those).

So to answer your question - it's out of the scope of your unit tests whether an external service like Google oAuth works. You just need to tests that you make a correct call to it, and here's where Mock comes in handy. It remembers the call for you to inspect and make some assertions about it, but it prevents the request for actually going out to the external service / component / library / whatever.

Edit: If you find your code is too complex and difficult to test, that might be an indication that it should be refactored into smaller more manageable pieces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM