Mock it right! A beginner’s guide to world of tests and mocks.

Video in TIB AV-Portal: Mock it right! A beginner’s guide to world of tests and mocks.

Formal Metadata

Mock it right! A beginner’s guide to world of tests and mocks.
Title of Series
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
Slide rule Presentation of a group Software testing Unit testing Mereology Cartesian coordinate system
Presentation of a group Personal digital assistant Software testing Unit testing Social class
Software developer Client (computing) Representational state transfer Cycle (graph theory) Cartesian coordinate system
Email Pulse (signal processing) Complex (psychology) Presentation of a group Hoax Multiplication sign Execution unit Set (mathematics) Real-time operating system Client (computing) Parameter (computer programming) Circle Endliche Modelltheorie Social class Exception handling Boss Corporation Software developer Moment (mathematics) Interior (topology) Unit testing Message passing Website Cycle (graph theory) Clique problem Resultant Point (geometry) Dataflow Functional (mathematics) Implementation Maxima and minima Field (computer science) Product (business) Revision control Internet forum Software testing output Message passing Modal logic Execution unit Cellular automaton Uniqueness quantification Expert system Line (geometry) System call Word Personal digital assistant Logic Object (grammar) Library (computing)
Point (geometry) Email Slide rule Presentation of a group Implementation Game controller Hoax Real number Patch (Unix) Multiplication sign Execution unit Source code Maxima and minima Streaming media Parameter (computer programming) Mereology Perspective (visual) Product (business) Different (Kate Ryan album) Computer configuration Single-precision floating-point format String (computer science) Feasibility study Operator (mathematics) Energy level Software testing Diagram Endliche Modelltheorie Social class Exception handling Dependent and independent variables Stapeldatei Software developer Expert system Menu (computing) Instance (computer science) Unit testing Cartesian coordinate system Symbol table Message passing Personal digital assistant Convex hull Right angle Object (grammar) Damping Reading (process) Library (computing)
Purchasing Implementation Presentation of a group Multiplication sign Modal logic Execution unit Set (mathematics) Mereology Magnetic stripe card Product (business) Inference Different (Kate Ryan album) Energy level Software testing Stability theory Exception handling Social class Dependent and independent variables Stapeldatei Graph (mathematics) Sound effect Line (geometry) Unit testing Variable (mathematics) System call Statistical hypothesis testing Type theory Arithmetic mean Integrated development environment Personal digital assistant Order (biology) Right angle Object (grammar) Simulation
Implementation Functional (mathematics) Code Patch (Unix) Interface (computing) Direction (geometry) Uniqueness quantification Source code Execution unit Sound effect Mereology Field (computer science) Product (business) Word Phase transition Software testing Right angle Game theory Error message Reading (process) Library (computing) Exception handling Form (programming)
so everyone my name is much Atlantic I came here from poland currently I work for sticks next features that because by Council for a house in Europe and today I will talk about Morgan botched the tools that help us to create testing invited so that me how many of you create unit test when you create your applications these slides so handy for the wall almost everyone might even everyone and how many of you know what the smoke and part and you use modern botched here you need wise great so almost advantage would be much easier to think about it when you know it already before I set my presentation I would like to answer the very important question why are we here right here
because I 2 but look a brings for those who answer my questions or if you try to answer my question so please doing the presentation I would have a few questions to please try to answer those questions because I don't want to link to have multiple genes after the presentation by myself I want to give to get so this is the crown for the presentation 1st I will show you a class which would be an excuse to create some unit as during the whole presentation thanks I will show
you the of few new test which will xn during the whole presentation then I will tell you what is more and how can we use it in our unit test for this presentation and finally I will tell you about the botched what this income can use it in our case so it's
a it's a client came to us and he asked answer to create an application which will receive the mice and from those in mice will take some data and then send it uh to some endpoint using REST API so it seems to be every single application so so we say OK we will do
it would take our best development cycle that major developers and after a few days they create a called very good called probably this cult would
contain a class smear out to this 1 must the Mrs. center has sent about which takes message as an argument from messages that takes it send the receiver and subject and this data is being sent in few by boss metals from request library In exceed the optimum so now so depending on the result from Boston adult-only request library there's method return true or false uh depending on the results to question to you uh what kind of unit tests would you for this sentiment of and this is 440 to link and ideas this so it has on yeah of course uh this is a very important but at what other cases this is basic I think everyone would agree this like this yes the the the exactly most models can rice exceptions and exception exception in this case would be realized by some mental because we don't catch it so on of cases is still take fair exception would rise quite uh this of a 2nd I think the most important thing is just to check if we really sense of the correct data to the correct and point because we use both model so as the the main thing that this function last is sending data to the endpoint so 1st of all we should check if hospitals exceed all this sentiment of sense correct data to correct place but when we execute all unit tests we don't want to send this fake this they to production and point because our client will not be happy with that so when we check if this pulse mental works correctly we also should not use their bodies are implementation of both metals so 1st think take this but don't use the original post mental 2nd think yes take if exceptional raised by post mental will be nice the outside of the cell function what then we have also yes and have the yes of course and that's something that we need to cheque causal yes of course you have some logic in in our mental the logic is that we try to sense uh they up if you want them so I think we should also check if we really do it if into objects in case of failure we try to sense uh this data and nothing but these we have message which is implemented somehow so in Europe we should cut off from the real time implementation of the message so a lot of things so let's see how we can do it is the implementation
of message passing is well single across from the fifties receivers and the subject and this is example of our unit test which during the whole presentation as you can see 1st we create missus of the object then we get the fake message will talk in a moment how we can get the next we try to send that and then you make some assertions for the results how can we get the fake message in this simple case we can just implement a new class inside our unit test with those few fuse and that's all it will work for now but what if you few days from now someone will newfies flow origin message class of for example and I think is more important someone with other new metal currently there is no movement at insight about someone of it and if we would call the path of creating a word a new class of the site of this unit as we need to extend the so we can faked assume adult and actually also check it dismantles called correctly from the production called this is very important so OK we can do it will spend some time on it and after a few days probably a clique would come to us and say while my let's use it in other unit test so we will do it and after a few days away at each other through a common see wow what a great tool let's let's make it public so under ninja developers can use it and that's when you notice that all something like this where the exists is called mock the so the 1st thing I want you to remember from my presentation is that if you need to pass a fake object insightful production called and you can do it easy thanking here we created fake message and you can easily pass it and then you smoke the what is not I think you already know it has also on said that you use most so I would just show to show it this is how you create mock and a lot you can call anything and anything that doesn't exist so if you do it for example not existing fees will always get value and if you call embattled which doesn't exist will also look at some i is very important because this advantage and disadvantage both together in 1 time but they will tell more about it later so 48 as we don't want any value to be redundant want some value for example find like this we assigned value that we want a mock to return what this simple line the and from this moment this value would be always returned by a mop this is a simple version 1 you can do it in 1 line they how can we use it in our uniqueness is very simple in unit that we create a mock next we assign a value which we wanted to return in production called the MoC should be executed and in unit test they you as we can make assertions if it was called correctly in this case our existing method was called on only once and without an argument so we can use a circle once with and if our model would be called with some kind of agreement we can do it with the same assertion but we need to put the value that we expect to be cultivated down more complex cases for example in models called a few times with different arguments that you can use a set and a call and of course in by on mentation find much more examples which are not able to show you in this short presentation I can show you just a few which have peak for example if we call data tumult and then we some metals some more we can use for example call Alex to check for older data it calls to the more Michael calls to check calls to the methods defined on the walk or we can have all of them using more calls in unit this you can use it in assertions but 1st we need to get that call object and we can compare existing calls with of expected called objects is how we can use it In unit that's so going back to our unit test we receptacle create a fake message we can do it like this is a model to which returns the fake forward message using a more is not defined we see this in the subject and this will work but is for a few days so and ideas is smoke is in the best pocket that we can create and this is clearly that the market the so it is that yes what you get for exactly this is the problem so spent is almost what I was expecting to hear and the best answer is I was showing him moment so what you said is that for example someone would change the message of the original message passing for example removing the subject fields and she would forget to remove it from the production cycle so it's the of coltan production called then our test will fail alright so they all or not you exactly but this will pass because you can call anything on a mock even if it doesn't exist and this is the problem this is where it should not create mocks like this and experts is not enough in this case we can do much
more we can do this 1 more thing so how I wanted to say that diagrams will come in this case because which something in production fold and all unit tests pass so everything is going so we decided OK we can go to the production can reduce our application and suddenly it appears that after the single application doesn't walk so it's much too late of course someone will say that you should use staging another testing instances this is just an example that we change the cold you need this past but production goal doesn't fall
Japan's with OK so the best mock which we could create in this case is with what you would called Spitzer and this will protect us from creating fakes of objects with feasible metals that doesn't exist in the real implementation In this case of my but what if a message is defined like this so all the fees are defined in In each mental and not on the class level can we use that set in this case the OK we unfortunately unfortunately cannot use but set in this case and this is also something that happens with the response which is returned by both methods the sponsor is defined in the course library and read cannot control it but we would like In our unit test to control the status quo the value is that you know we could do it with but the problem is that implementation of response looks like this so was called is the finding in each metal and not on the class level so we cannot use Spitzer set when you you want to control when we want to create a mock of response we need to do something else and this is that so experts doesn't check if uh what we try to create with more for example to that was called exists in their in their implementation of the recast it all what it does is taking if what we call from the production called was defined in the mock so it's not the same but we get some protection so perfect but that's what we have to do the OK so we already know how to mark the response we know how to my not the message now this talk about the most important thing how to move the post model because we don't want sensors that as they come to their real production and point the as I said we should use more when we can easily pass them the production in this case we cannot do it easy it's somehow have called it but Mr. send balloons for us so we can ask how a ninja developers to change the source told and for example we can define sender in union but and in our tests we can create a more flowers and and that's everything will be fine but what if must assess the come from some kind of library and you can exchange it but we still need to do this more so this is the 2nd thing I would like you to remember from my presentation you spot when you cannot easily the fake object so this problem how can we use a partial there are different ways of using patches that I prefer using gets as on a patient with this book and this is 1st option of the solution you can say what this thing what should be part and all of that's the cultural says that we want spent set up people to to be used batch would create a mock for this object and will pass it as an argument to our unit test and in in a few we can define what should be returned uh from our Malta and the end of our you as we can make assertions to our work there is a 2nd option of using partial on operation and picture I prefer this solution because it doesn't take stream it takes an object that is for the 1st month and 2nd plant can be also replaced by not being but I don't want to show it on the slide because it would be a little too complex so I prefer this solution because when they use Aidid it supports me when I do some kind of a Victorian with the importance of other things and in this case D would have been with fixing those symbols in case of strings it would not take anything when you create a part of this very important thing that much created the but should be created based on the place where we call their parched mental is something that you can forget when you create a part for the 1st time we have almost to the same cold the cold told on the left we call perspective from request and Dakota on the right we called post but built from from something like Mr. 7 the both both models that achieve the same they come from requests but the differences in the way how they are called and this makes that we need took part in the defense the on the left the patch was mental from requests on the right the patch hospitals from message sender is very important we can forget about it when it created but as for the 1st time but that we already know how to much all or maybe more Postman built so now we can go to another thing how to make it to rise exceptions and to check if exceptional suffice we can do it simply of course with a pouch the and we can do it by assigning exception put fact that and thank you we can check if
exceptional stripes another thing we have a post called a few times if it's of course necessary and you'd like to check if we do it in our culture so in case of failure when we tried to send the data for the 1st time I would like to check if our culture twice stability next time next time so we need to be able to turn to return different things in our unit testing for different calls for example for 1st time return failure for 2nd freedom fighter and for 1st time without success how can we do with of course you can do it without much and of course we can do it with side effects we just assigned to cite difficult you will and if you it as still further responses that we would like to be returned by dismantled and 1st value from the be done for the fiscal 2nd value for the 2nd goal and so on sometimes we need especially implementation of our original metals and nothing goes can walk so in this case you can do it we can have our especially implementation for example defined in our test and you can assignment with side effects is that of all these tiny implementation of our post what you want to stay on this you you you are you know we still you mean disgraceful all what was it used to put that right and you certainly not all of them the you human types and folks yeah so I yes an opposable yes I is that the at the end of yes what I want you to take thing and existing fees maybe in this example it's not the best example some other example would be a better for this case but look at the the rest of the yes the graph so there are different uh uh wasteful this people say different things the exact inference OK so I think this is the next like sometimes we integrate a few purchase to 1 unit test and this is the simple as you can see but you need to remember that the order off MOX which are passed to the unit test are opposite to the order of watches which are not based this metal so 1st batch return more which will be passed as the last 1 to the unit test and the last part will return them more which would be passed as the first one to the Continental the sometimes the following you need this that has the same just so instead of repeating them all the time the above each unit as we can just yeah and set it on the class level yeah yeah and this is a very important you need to remember about this 1 before each test this apartment this called and after each test 2 dome atop the school to donors useful to some cleanup of the day uh you need doesn't set is used to prepare some environment for you this but people often forget that if set up with rice exception then you need tests will never be executed and the don't also will never be executed so if someone was set apart in the set up metals and you would think that she can clean it until it down and just after setting up the parched there will be exceptions rice in set my the 2 dollar never be called so if you made this may mean mistake for example in 1st a new test and we acute 1000 unit tests and 200 1st unit test is using this part but he this has doesn't know that this you think about it may fail and you spent a lot of time to find out what happened so to solve this issue we should not use the dollar we should use but you know that a like this and we will be protected from this problem OK and last think I wanted to show you a very simple and I think this maybe will make you check that accommodation for Marx in part because you can fight a lot of interesting things that how we can simply more environment variables with just 1 line like this using part but the OK so 2 things I wanted you to remember after my presentation 1st think you smoke when you can simply pass the fate of objects to production cold and 2nd thing you patches when you cannot simply passed the fate object
to your production of and missions and to be thank you yes and it has you can is there really a tool use like direction on the amount and just over right of should he's not so not only revised underrun but to it's in the dictionary yes you can the whole had it is stated that accommodations use the example in the the and what questions at which they you it's more questions veridical you have some function and you will want to test it and you walk stuff outside of this function and sometimes what you have is you have a few more books you have defined side effects you define may be something else you have to check more lots of these and other stuff like is there any normal way how it's possible to make it look better so is education of his say you have a lot of walks with a big step with minus side effects and everything can 1 unit of yes not is low for example the form what's already looks like quite a lot so 1st of all Triton not to the because it's too complex is that it would be very hard to analyze it so try to make it more more simple use of but but how it depends on the culture I would have to use the cold and maybe uh there will be a solution for this but of course are not always as possible the game the and more questions this so when you're mentioning the common error when patching there that you need to watch the wording means you have uh from by Python I is in that is no that you're coupling the implementation of the thing you're watching with your test basically you need to go to the source of your library yeah and check what they're doing and they might be changing in minor patches because this is not necessary public interface so make your code brutal it's very good question I like it very much so this was only an example to show how we can uh check if you're in uniqueness if exceptional size I'm not saying that that the court which I show you the production called was the best they're not different ways for this and I think most of us will agree that we should catch this exception in our production coatings such metal and buckets with our own implementation of exception for example and rise this exception and then we would catch our own exception and check this hour all exceptions so we are not going down to the library which fields and I think this might be answers to a question on my part OK yeah and questions the the so I think less than 12 people were very active during my questions so please those people come to me and some other additional people please come also because I have 12 things and they don't want to bring them it's too much from up 1 more question so and they are reading so I just call them look the because it sounds funny and is almost close to what which is which exceed they are so that readings inside the would yes at and the so the phrase that depends on you if you if you are a beacon then maybe said the phase will be so we have a yes exactly you have to