3 ms·
You can create a variable, set that, and pass that variable as the first parameter to greeting. I don't see why having a class makes testing any easier.
by quotemstr 15y ago
You can create a variable, set that, and pass that variable as the first parameter to greeting. I don't see why having a class makes testing any easier.
- radicalbyte 15y agoDecoupling. For example if your code requires access to an outside resource (database, message queue, email service) you can mock said resource in order to test the code in isolation.
- skrebbel 15y agoHey! Ho! Stop that. Nobody uses the word "Decoupling", I just learned that in the video. You must be pulling a fast one on me.
- tmorton 15y agoLet me clarify what I think you're saying. It's not about testing class X, it's about testing code that uses class X. As long as X is a class, then we can create a mock version and pass around instances of it. If our "X functionality" is stored somewhere else (ie a bare function), then that gets harder.
- quotemstr 15y agoDown that road lies the hell of dependency injection frameworks. A simpler approach is to use something like detours[1] to mock bare functions directly. [1] https://research.microsoft.com/en-us/projects/detours/ https://research.microsoft.com/en-us/projects/detours/
- Drbble 15y agoGoogle Guice is not hell.
- densh 15y agoIn python you can mock pretty much anything you want. You can mock package-defined variables by simply modifying them to the value you want (sys.modules['mymodule'].MY_GLOBAL_SWITCH = True). In such a way you can even mock standard library functions and built-ins (which have their own __builtin__ package). With such capabilities you can test pretty much any code. (There are some limitations indeed, like you can't change types which are defined in C code.)
- johnkchow 15y agoI agree that in Jack's specific examples you don't need the constructor: the constructor trivially saves the parameters into instance variables. However, one thing that hasn't been mention is when the setup of the class is non-trivial, which most likely requires breaking it down its helper methods that's only specific to that class. Exposing those helper methods outside of the class can potentially add as much unnecessary complexity to the code base (i.e. is this a helper method for another method or if this is standalone method, how does this fit into what I'm trying to achieve?). Yes, this could be mitigated with good documentation, but classes exist for this specific reason.
- quotemstr 15y agoHuh? Modularity has nothing to do with classes. In plain C, your main "method" can be a simple external function, and the "helpers" can be static functions. Python offers similar functionality. Making a class is a step backward because the internal helpers have to exposed to users, even if they're intended not to be used.
- Drbble 15y agoExposed how? Methods can be private.