[英]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. 目前,我在尝试对Google oAuth Picasa身份验证进行单元测试时遇到了麻烦。 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? 但是,如果我使用Mock,我怎么知道该功能(即Google oAuth Picasa身份验证)确实有效?
Or, aside that I develop unit testing after the app finished, did I made other mistakes in understanding Mock? 或者,除了在应用程序完成后进行单元测试外,在理解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. 因此,回答您的问题-超出单元测试的范围,例如Google oAuth之类的外部服务是否有效。 You just need to tests that you make a correct call to it, and here's where Mock comes in handy. 您只需要测试是否可以正确调用它,这就是Mock派上用场的地方。 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. 编辑:如果您发现代码过于复杂且难以测试,则可能表明应将其重构为更小,更易于管理的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.