简体   繁体   English

模拟对象如何替换正在测试的所有系统功能?

[英]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.

相关问题 如何在 python 中使用 unittest.mock 测试 class 中的 object - How to replace an object inside a class being tested using unittest.mock in python 如何模拟在同一文件中定义但未被测试方法导入的函数? - How to mock a function that is defined in the same file and is not imported by the method being tested? 如何模拟与正在测试的方法相同的 class 中的方法? - How to mock a method from the same class that the method being tested? Python 模拟:如何模拟从正在测试的 class 方法调用的 class - Python mock: How to mock a class being called from the class method being tested 如何在测试函数中创建对象使用的内置函数模拟 - How to create builtin function mock used by object in tested function 如何在测试方法中模拟受保护/私有方法? - How to mock a protected/private method in a tested method? 如何用模拟对象替换函数以在Python中进行单元测试 - How to replace function with mock object for unittesting in Python 到底是什么被测试为“不正确:”?如何测试? - What exactly is being tested as “not True:” and how? 如何测试记忆功能? - How can memoized functions be tested? 如何仅在经过测试的类而不是在单元测试的情况下模拟函数 - How to mock function only in tested class and not in unittest case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM