Mocking in C# Unit Tests - How To Test Data Access Code and More

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
unit tests are great but how do you test your data access you shouldn't want to actually delete records when you're testing the delete method you definitely make sure that that method is configured properly this is the type of problem that stops a live developers from creating unit tests in this video I'm going to show you how to overcome these obstacles in order to write tests that handle working with external dependencies like databases now if your newest channel my name is Tim quarry and it's my goal to make learning c-sharp easier this channel is full of videos explaining the various parts of c-sharp I also have a website where I provide full courses on c-sharp and sequel you can check it out at I am Tim quarry comm then it's also where a blog post for this video is located it includes source code for my sample application both of the start of this video as well as at the end also note that in the description below you can find links at joy mailing list or support me on patreon okay let's get started in our example project notice application is a bit larger in scope than some of my sample applications that's because I wanted to give it a bit of a real-world feel what we have here is a working console application that reads from and rights to sequel like database it uses auto fact to Hale the dependency injection now cover these two topics is outside scope this video but you can find videos on each of these on this channel you can also find a full course on sequel light our website now the last piece of configuration this product has in it is a X unit project and that's where we have our unit tests so let's go right to the X unit project called mock demo tests so in here we have a folder called logic which corresponds to this logic folder here and we're testing the person processor which there is a person processor right here and this is the person processor tests class now one of the things you might notice is these green checkmarks next to everything this is what's called live unit testing this is only in Visual Studio Enterprise Edition so if you don't have this I'm sorry it's a really cool feature I debated turning on or not this video because I don't like demo things that you might not have access to or you have to pay for however in this case I think it adds a visual element to what we're doing so I'm gonna leave it on for this video but just note that while you may not have these green check marks here yours will still run the difference is that when I make a change live it will update these tests for you what you have to do is go a test Explorer and either run all or click this little box here that says run tests after build which I recommend that by the way now what we have here is just three actual tests now I've got three in line via statements here I got a couple more here and then I've got four more here so as nine total tests which the test explorer verifies for us so I'm gonna leave that closed we that just take up real estate but what I have is I'm testing the convert height text 2 inches method passing in various height texts and I'm also testing the create person successfully method and the create person throws an exception method so both the it should work and the it's not going to work and these are pretty standard tests and if you go to a person processor you will see that I'm covering the green checkmarks by the way do you know what coverage so I'm covering this create person method the the constructor and I'm covering the convert height text to inches now a couple of things here first of all this is a tuple which if you are in I'm using dotnet 4.7 and so those tuples built-in however if you use using an earlier version of.net then what you need to do is you need to install a new get package for value tuple that's the name of the new you package so he went to your references right clicked manage nougat package and you browsed for value tuple all alert there you go you installed that package if you're using a version earlier than dotnet 4.7 you can tell that by going to your properties of any of your projects and looking at the target framework here and that's four point seven now if you don't have 4.7 but you do have Visual Studio 2017 you can install these so if they don't come if they didn't come with your Visual Studio you can install the later their download packages just google for the.net framework version target framework version okay and I recommend doing four point seven just because you get some nice new features if you don't go for point seven point two is great I didn't with this ice went to four point seven just so I can use tuples but take every one of your projects to that same level so four point seven for demo library and then also for my mock demo tests project so all three are at four point seven all right the automata note is that what couple things first of all for all three of these i'm create a new instance of person processor in order tested however person processor if you remember takes in one dependency the i sequel light data access dependency and so assigns that to an internal variable however the create person method as well as the convert height 2 inches method do not use that underscore database dependency so therefore when I create that instance I pass in null for that that instance okay so I don't need it so I spaz adult that works and will go right through now if I tried to use that underscore database for something in one of the methods that I call that would crash the application but because I don't need it for these tests I can pass in no it's fast it's safe it works and actually if it started crashing your unit test that means he did something different like you brought in the underscore database into another method and you didn't realize it and that's okay that crash is your unit test or breaks them because that means that you've changed something you need to clean it up so that's okay okay now the other thing I want to point out is the code coverage so let's come down here and let's comment out this line right here and if we come at the person processor right there validate name notice how this line right here line 117 has a dash and it says covered by zero tests so his code coverage is really helpful now if you don't have Visual Studio Enterprise that's okay there are third-party plugins as well but do this if you can get your hands on legally get your hands on Enterprise Edition of Visual Studio and do some testing this is a nice feature to have because it shows you right away hey you got a problem here in this case I had written these tests and hadn't done this last test here the test is to pass in nothing for a first name and by doing so it says oops that output is false however since I commented it out it's no longer being checked by any of my test methods so just point that out that there's a a lot to be said for code coverage and making sure that your tests are covering all a different scenarios now the rest of this application we don't really need to care about now just so you know the console UI does run it does go there's no test for it it does go through and it creates an AI application and it actually runs and it says okay what do you want to do do you want to display the people add people or something else and so it does work there's the actual database so if you run this it would run in fact let's just do that real quick just to show that in fact it does do something if I load people nothing comes back because there are no people okay and now if I say exits I hit return it closes out so this application does run it does work and does write to an actual database now it's just a demo database and it does write to the bin directory so if you did a a build rebuild solution you can clean out that database and started over alright so that's the application that's what does it does work like I said this is a demo project demo library we're going to cover everything it does because the real point here is not to worry about the application the amplification is great and it works woohoo that's not the point the point is to focus in on this person processor because we have a problem that problem is represented by these three methods load people save person update person well these three do exactly what they say they're going to do the first one loads people from a database the next one saves a person to database and the final one updates an existing person in the database these methods are really simple you don't we don't do a whole lot in here but we still want to test to make sure that what we're doing is what we intend - the problem is that if I test this method save person and I test it with an actual database connection then obviously the new person to a database every time the solution you may come up with is well what if we create a sample database and then the unit task just wrote to that instead and that could work the problem with that is a couple things first of all you have to keep a database in sync meaning if you change a table in the production database you have a change in the unit test database next you have to have some sort of cleanup script going on because if you're always saving people that David something like huge especially in my instance the live unit tests the live unit tests run anytime then make a change well if I make lots of changes I'm writing tons of records to the database now what if you had delete now I have one here go if he had delete person method well if that ran more often the saved person then we might accidentally wipe out the entire database and all of a sudden the load people method would start failing because not loading any people there's no people in there should that be a problem so it's caused these cascade problems if you're trying to write to and read from a real database so what we're gonna do is a thing called mocking mo seek a mock and what a mock does is it basically pretends like it's something that it's not in this case we're gonna pretend like we're the database okay so we have this dependency in this class on I sequel Lite database data access class now the really cool thing about dependency inversion and using dependency injection to help you with that is that you don't have hard dependencies where you shouldn't have in most cases and so with no hard dependencies or very few then you don't have to pass in the real I sequel light data access class you can pass in your own you can create a separate one a different one and what happens here is I passing a different one you can say yeah don't actually write to database now you see me do this when I create a demo project I might have a save data method but it has a console.writeline and that's it and so it's pretending like it saved the database but it doesn't actually do it that's the kind of class you can create and then pass in to the constructor of person processor instead of the real one and that's what we're gonna do but if we had to write a whole new class that implement this interface that'd be a pain it really it would make our mock demo test class a whole lot bigger because now all I said we're replicating every single instance where we're passing it around so we're gonna do mocking which pretends like we have a full object we don't really need that full object so we'll get that in just a minute now the other thing to point out because I want to assure that I don't gloss over this or how you see something and then and then that realize what we're doing here that is a dependency on the person model we're not passing into the constructor and so you may wonder well why you doing that well why do you have as dependency and the reason why is because this is a model and this just holds data it doesn't have data and processing and so because of that I feel comfortable instantiate a new object just for data and so that's okay it doesn't violate or not really violate the dependency inversion principle okay so just be aware that that was intentional it wasn't like I was saying oops I just you know kind of glossed over that and said yeah I'm not doing dependency inversion I am doing the panthéon but this would be outside of that scope we can talk more with that later but I want to point out before we go on okay so let's get back to how do we test these three things we're gonna start off with well it seemed like the easy one but I tell you what all three of these pretty much the same thing okay so let's deal with load people first we'll start and I just like to do this let's copy and paste and that's actually for now we're going to change to a fact if we want later can change back to a theory but a fact it makes it easier because it'll have to pass in data and figure that out yet and let's look at the name of it should be load people underscore valid call some like that okay naming is hard especially on unit tests is what we name especially when you had like these theories where multiple things can be different so it can be challenging to get right or get right enough okay so let's wipe out all the code inside here and throw a new not implement exception just we have something on the books for a test and notice right away I save that because oh you got failing test which is one of the nice things about live unit tests it right away giving feedback and says yep that's not right so let's make it right but first we have to set our mocking because if we look back over at person processor it uses the underscored database which we know is that let's kind of hide these or trim them down a little bit it's the ice equal light data access instance they're passing at the constructor so we're using that and using the load data method it's a of type person model or in the sequel string so back over here we had this passing of null well that's not gonna work because we're actually trying to call the load data method so we actually have to have an instance of our interface I sequel light data access and this is where mocking comes in so let's right click on references for our mock demo test so right click on references and say manage nougat package we're gonna browse for m oq this is a type of mocking framework called mock and yes that's a great name for a mocking framework mock let's install that and install a 4.9 version which is the latest stable version well install that it's going to Cali dependencies install the package we're going to accept the License Agreement and it's bringing some other packages that needs before we go out of here though I'm also going to grab this Auto fact extras dot monk and what this is is an extension for auto fact that deals with auto mocking of dependencies or of mock dependencies and since we're using Auto fact is our dependency injection system we're gonna use Auto fact extras mock accept those terms as well and now if we see the the installed items we have a number of installed items and if we go to updates we'll see we have some updates to do let's go ahead and update all of these we don't have to and in fact there's that value tuple that brought in that's probably cuz of one of our dependencies and you know what I'm going to yes go ahead and say update all I like to have my dependencies all updated before I take an application into production because when I do take my application in production then I might have a problem continue to do updates I don't make sure I test out early and often that the latest updates work for me so that when I go to app go into production I don't have to go okay now I have to you know check everything check at the beginning before you deploy try and update to everything the latest it gets a whole lot harder once you got active code on a server somewhere so let's just do a latest update I'm going to build the solution make sure that builds fine and it seems to have succeeded my error list is empty that's good so now we're ready to get started with mocking so the first thing we do is we create using statements now using some is different here than it is at the top this using up here is a shortcut and so instead of saying let's say system instead of saying system dot console dot write line we just say console dot write line because II have a using system up here it's a shortcut that's a different using statement then down here this using statement is you actually using an interface called I disposable anything in here that gets declared inside his parens has to be of type I disposable and the reason why is because at the end of these curly braces it's going to properly dispose of that resource it's kinda nice it's a way to make sure that we clean up after ourselves let's save our mock and I won't see Kaena mo q equals Auto mock do a controlled dot there how to use daemon Auto mock dot you get loose now it's some debate here and so let's talk about that the get loose versus get strict the difference is hard to understand when you're first starting out let's put it that way but the difference is that we're going to task to see if a method is called with get loose if the method is called we're good however with get strict what's gonna do is say if that method is called cool but also no other methods we call so it checks both sides of it the this one was called and the other ones were not and so it was get loose it doesn't check the knot side of things then we can't ask parts of a method or test certain function now they have a method without making it very brittle because with get strict it's kind of brittle where if we make one little change it breaks half our unit tests we don't want that to start off with maybe down the road you change to get strict but for now get loose we'll get you started and it's easier to understand what's going on so what this has done is set up this variable called mock and this mock is an auto mock and what that is is a framework for creating fake items let's just start off by actually creating one Czar's a mock dot capital mock and we're gonna mock the I sequel light data access interface okay so we're gonna do is whenever you need an ice equal light due to access this this version it's gonna be passed in instead of the real version so it's kind of hooks into auto FAC and so the auto effect goes to wire things up goes oops I can grab that one instead so let's do it next is a set up so the set up does it sets up one or more methods for us and this is actually going to be an action where you pass in basically a method so we're gonna say X equals greater than for the arrow and X dot and notice how we have some methods here beyond the standard ones we have load data say a and update here sound there well if looking at the interface and saying what methods do you have and it has these three methods and so now we can set up to say okay this method here is going to do so we're gonna look at the load data method and it says okay what do you want to pass in 40 or what is going to pass in 40 well in this case it is the person model how I know that well because right here we pass in the person model and the reason we're doing this this way is because we've actually set up different setups for load data for different models if you wanted to but in this case we're just gonna do the one because that's all we have and said okay what's a sequel strength being passed in well we know the sequel string is the select star from person actually not paste that in there okay so that's we expect is for to say select star from person so now this method load data is set up so it's gonna list listen for the call and if this call is made it's going to use this mocked version instead but that's really helped us yet because what we're really interested in is the data coming back and so we a do is say dot returns well that's the return is a list of person model okay so that's what returns and so we're gonna do is pretend like this method actually returns that list of person model now maybe a little bit lil bit lost this point don't worry finish out the demo I'll come back around explain again but I want to show you first was doing I'll talk you through it if you have to rewind and watch it again afterwards it takes a few times to get mocking but once you do and once you start using and that's the important part I want to start using it you'll see it's not really that difficult in most cases it's more just a little quirky okay maybe what's happening here is we're doing some of the the job that usually goes on behind the scenes of dependency injection but instead we're doing it manually so it's a little bit quirky but don't worry now what we have here is a return statement but we have to return something we have to return a list of person model well what do we do with that list how to get that list well we actually create it so I'm going to create a private method private list of person model get sample people whereas say oops private void no right listen pill why this yummy an area I have my myuser thing that didn't have a generic in there right there didn't have a user to it so control dot fixed it for me we're good to go so a list of person model output equals new list a person model will say new person model and its first name equals Tim last name equals Cory and really these models don't have to be full-featured so for right now I'm just gonna do this I'm gonna create first and last name that's it and we'll say that's my wife my one son and my other son okay there we go there's four people it's my whole family and now we just return output so this method now just returns a list of people so we're gonna call this method inside here so now we've mocked out the low data method when it's called with this parameter we return back this list which is actually this list down here this list of four people so that that mocks that method out now we can actually use it let's go ahead and use it to do that come down here and say mach dot create this it creates a person oops not person model person processor so CLS or if the controller you know in your API RS went that would be ctrl maybe or you just call it the you know whatever whatever you want to designate for the class that you're testing in this case I think it's called CLS so what happens is it uses the mock to create the person and processor class so it's in a person processor and it's going to run automark but whenever it asked for I sequel like you to access it's not gonna give it sequel like data access it's gonna give it give it our mock to version which has just this one method wired up and that's the important point here we didn't have to wire all through methods oh just the one that this method is going to use so person processor just calls one method load data therefore that's all we care about we don't care about save data or update it up they did know yet so let's come back over here and now we have our class to use so we can say Steve are expected equals get sample people and now our actual equals CL s dot load people so my expected goes down to this gift sample people list or this method and which generates the for people and it's expecting the same list and expected as if we call the class load people and we get the actual invest list so it seems like we're not really doing a whole lot here we're just comparing we're going to compare the list we created here to the list that we created here doesn't seem like we're doing a whole lot right well actually we are and so let's get into that so let's start with our certs assert dot true actual does not equal null that's important because if the load people method fails then we have a problem let's get rid of our throw exception and notice green check marks kind of cool now as test passes because actual actually return a value we're not done yet assert dodds equal expected dot count actual dot count let's make sure that the number of record returns are the same why they'd be different well what if in here we're doing some kind of modification now this is a very simple method we're gonna change in just a minute but for now we're just testing that it actually calls select star from person and that it cause a load data method and it returns the values that was given that's all we're testing now what were to happen if we was said from person's check that out we've got a failing test why is that well we checked to make sure that the parameter going into load data was select star from person not persons we changed that sequel statement it breaks our call is that okay yes that's perfect the or testing is they were truly making this call the way we expected it to if we decide that that's okay we can come back over here and change our unit test but it's not okay so I made a mistake someone you know may the cursor in the wrong spot and they hit asked and realized the wrong spot somewhere else they didn't actually take away the S it broke a unit test we cuz it's not what we expected change that and all of a sudden now our test is passing so that's a good thing next let's look at what happens if we refactor this method now unit tests really help us in the refactoring phase because we can feel confident making changes inside this method if it's properly tested because any time it could change those meth does tests we run against the method as long as the output stays the same then it should be fine so let's do this let's grab that list of our outputs equals let's grab this right here and say return outputs which so far so good I've refactored and put into a variable first but now let's say we said we're making some changes here for each item in output if item dot first-name equals Tim then whoops double equals then we need to make sure that let's just make a change here item dot first-name equals Timothy okay now the unit test passes because we're not actually checking that data so how do we do that because that's not what you want we're making a modification to our code see down here Tim has intake change of Timothy well we can't just say assert dot equals equal expected and actual can't do that that's gonna break now if you said well if you comment us out it won't break in more it's not true okay watch it again still broke it the reason why is either trying to compare two objects and you can't really compare obvious by saying are they two equal you need to check their values and do a value comparison so instead of this we could do is do a for each actually let's make it a four no we don't do four but because we know the counts are the same we can do a comparison this way because they should be the same order even and actually checking the order is important so expected dot count so we're asked to assert dot equal expected dot expected position I dot first name actual at position I dot first name that's comparison the first name notice we have an error right away if we comment this out we're all good see the difference right there just by checking the first names for each item are the same we found that difference last name last name okay so we're all good now but assume it's refactoring we can see that nope it brokes something because we're making a modification to our data that we weren't expecting in our test again the question comes back is that okay or not what this is doing is it allowing us to test everything that happens in this method if changing data is okay then we check for that but in this case we didn't want them had changed it we wanted to just sit back in which case if errors out our test so that just kind of demonstrates that we're thoroughly testing this method now we're not testing is the actual data access because that's outside the scope of this particular unit this unit is just about loading people which just makes a call gets the data returns it it's all it does and so we're testing for those simple things if we're going to do extra stuff cool then we'll test for that as well but now we've removed the dependency on the database being involved in this process it's not involved anymore we're pretending like it is with our mock but we're not actually getting the database involved so let's go the opposite direction and test out what happens if we save a person or update a person let's come back over here actually I grabbed this test and copy and paste it because it's got a lot of same code that we need and since we have save people method so instead of load de Grasse say save data and it's going to not be a generic so we can take away this person model and it's not gonna return anything let's get rid of that and then look at what it's taking in it's taking in a person model object so let's do this this is where I I kind of reuse these methods let's take in a call this gets sample people sir si var person equals get sample people first nope it's not first just gonna the first one right like that okay so now we're getting the first person back from this method call so we're using it again in a different way so instead of saved a grass person here so this this text right here will pass this in the person which actually takes in the person also the sequel statements which let's grab the whole sequel statement let's grab the whole definition and you might ask yourself why are we just gonna copy and paste and compare well the reason is because first of all it protects us from that that typing actually before where you type an extra letter and break the sequel statement they don't notice it well this checks for that and it also allows us then to make sure that this is the method was called correctly we weren't passing in a blank sequel statement or a different one that somehow getting overloaded or something like that and these are simple methods but we're gonna kind of bring out some of more advanced topics by calling them that so we have a set up and that's cool but here's the problem and it's gonna pass because nothing's actually happening but that's kind of the thing is safe person returns a void so how do you test a method to make sure it actually worked to make sure it did with say I was going to do in this case very very simple is Jewish just going to save the data how do we test to make sure that's going to work well we have the option of saying oops not on here let's go back over here let's create our our CLS so VAR CL s equals MA create and it's the same thing that's going to be our person processor arrests ACLs dot save person and passing our same person object and now we're going to make sure by saying mock dot mock of ice equal light data access dot verify so we've done is we have created our class or class instance a person processor which we already have mock 4i sequel data access which just has our call here to save data and then they actually called that but since it's returning void we couldn't really capture any data coming back to make sure it worked so then what we did is we said okay for the mock of ice equal data access verify that we'll say X and the fat arrow or equals and greater then X dot save data person comma sequel was called x dot exactly once and this is where we have to bring in our user payment for mock so now you get rid of that code and so this code does is it verifies that this method call the savedata method call inside of our isequal light data access interface was called exactly once now if I said exactly twice it's say nope that's not right it was only called once and so that verifies for us is that we actually called this save data method once and that's what that loosed for such trick is coming in and so it loose I'm just saying you know what and actually loosen stricter to work the same way in this in this instance so I'm just verifying that this method is called once and only once that way if I had some kind of weird loop happening in here it would catch that so what if I said for I equals 1 I is less than 10 and then I made this call inside there and it says nope that's no good because now you called that 11 times not one time if I were a check for 11 times it's gonna go yeah you're good you you're expecting cold leaven times sure eat it but this catches the fact that I called it more times than expecting so that's how you test a void method you test to make sure that it does whatever expected action you would you were having it do the number of times you expected it to happen or that it happened at least once and you can do that I'm calling exact here but there's other options you can go with at least once at most at most ones between never so there's a whole list of options you can play around with to make sure that certain methods are called or aren't called depending on what you're looking for now this methods not really doing a whole lot of interesting stuff right now and so it's not only showing off the power as I said but it was showing off is how to do things I verify that a method was called but let's make it be a little cooler I got to good say so let's start off by saying this is good actually going to be new person model and we're at get a full you know ID equals one first-name equals Tim last name equals Cory and the height in inches equals 80 all right there we go so now there is the full person on it which just works because whether we get from down here or they create it up here it doesn't really matter but let's just say that now what's gonna happen is our method is going to modify the sequel string so instead of saying at first name it will actually say Tim well just put the actual value not the quotes around it which would actually break the call but let's just say it's going to do this but it's going to replace these items with their value for whenever you pass in and this is generally a little bit nuts I'm gonna put the put the quotes in sorry this is one of those things I hate writing bad sequel now this is bad in a different way if you actually have a method replacing your parameters then you're opening yourself up for sequel injection attacks so please don't do this what I'm gonna do here please don't do this actual work in a real application because that would make your application more vulnerable but in this case I'm doing to show you how to test for something like this all right so this is the resulting value which I expect if I'm passing in this value but right now it's saying nope that's not true because it says at first name at last and all the rest so now we have a failing test we could actually write some some code here where we say let's say sequel equals sequel dodge replace and we'll start off with at first name with especially strings not charged and it's gonna be the once because a stringbuilder are the string interpolation the single quote and then person first name and quote and there you go there's our first replacement and we'll have two more will have last name its last name height in inches is height in inches and hopefully that should all work and it doesn't it still says you know what it's still not the way we expected it to be and that's because height in inches shouldn't be inside single quotes but notice how it caught that we take that off it should be this which works and it does notice how we're getting green checkmarks now so we're checking to make sure that this method is called just once and that this sequel string has been built correctly and it has so that is how you can use mocking to replace your data access code or in a code that has to rely on external something you can mock it instead in order to not have to actually make those calls it gives you a controlled environment you need and it allows this test to run as many times as you want just in making these edits i've run these tests probably 1520 times that's okay because it's not actually changing data in fact if I read start this up and let's do that let's call load people nobody there even though I've been calling the load people method and the save person method multiple times so it still does work the way expected to it's not changing our database and yet it's testing to make sure things are working as intended so that's really all there is the mocking start off with you're using statement for Auto McDuck get loose and then use this mock to actually build out your interface calls and create the the hard code or the the real you know the real fake the real fake method that you then control and then either test to make sure that method is called or test the return back that you expect versus what actually gets returned when you call that method again mocking out that piece of it so that's really all there it's like I said I know it's a little complicated at first and yes this is a more advanced topic this is not for day one c-sharp developers if you are in your first three months of learning c-sharp it's probably a good idea to put this video on a shelf somewhere archive it come back to it after you have mastered things like instantiation and interfaces and some other things like that basic unit testing start there but once you're at the point where you create some basic unit tests and you're like man I need to get rid of this this dependency on my database so I can test things this is how you do it so may its archivist video early like that book Marcus video and come back to it when you next have to create a mock because the first oh I don't know 20 time to do it you're gonna have to refer back to something else or some documentation because of things like this verify that I did this video I almost always do it I try to put my verify up here wrong spot for if they haven't actually called it yet so that call right there has he made before I verify that it was made makes sense so that's kind of stuff that I forget unless I look back over it so even though I've created a ton of types coming back over them looking at an example something like that is really helpful because you're just not gonna create these every day these anyone's know while things now for ego it's more thing I'll point out before we're done and that is I ain't create tests for this this is my data access code now just so you know you might want to steal this code this is actually really good code this is code that I use almost every day sorry I'm not trying to own horn but this is code that I've kind of condensed and boiled down over time to the point where it really works really well but this code right here very small little method what is doing is connecting to sequel light and it's either do n query or it is executing a query that's it and that the corne query returns a to list that's all these things do I'm not gonna test these you may say oh you know for high percent test coverage I'm not concerned about hot percent test coverage and this is where a little bit of the the battle can come in over tester and development all the rest but here's the reality I don't test to make sure that dapper works and so what this is is dapper I don't test to make sure defe because dapper tests to make sure Deborah works so those are the kind of things that I'm not going to worry about especially not unit tests may be an integration testing which is a larger whole or even further up the chain you do a very few of those but for unit testing I'm not gonna test somebody else's work just like I'm not going to test to make sure that date time gives me a valid date time Microsoft has tested that thing like crazy I can rely on date/time to get me a date time that's what's how it is so there's some things you don't have to test and this little bit of code is one of them even rayet asked for this there's knocking a whole lot I'm gonna glean out of it did it make the call yes well but that's all it's doing anyways it's making that call so we don't really have to worry about this lowest level stuff but one level up or a call if this person processor is doing something that's where I make the the test that's where I test out my code is essentially not damper code my code I test that to make sure my code is working as I intended it to so that if ever changes for example if I go you know what I'm gonna put a D at the end of sequel here and oh it didn't work so I'll put it my cursor there and oh there you go and I forget that I actually put named down here look at that hey Brooke my test so I test my code not somebody else's and that's what this does now we didn't test update person I could do that the reality is it's pretty much the same thing as my my save people but that's it that's mocking you just have to add in your test project in references for nougat packages you go look for moq install mock and also Auto fact extras dot mock and that's a fusing Auto fact for dependency injection once you do you create your using statement to start up mock which is going to handle working with the panty injection and it's gonna handle the creation of those new mocked instances you actually create your mock for whatever that you want to X racked out and not use the real item for and then you either verify that it worked or you check the return value and make sure those return values match okay that's that's what there is to mocking at the heart of it it's not that complicated once you've used it a few times and this is where again I come back to my my premise that whenever you are learning something new you should create three to five test projects separate projects not the same project three to five full projects that are just as simple as can be conceived so nothing real-world just simple stuff not even this complicated I create a whole console UI you don't have to do that just create a little library with a couple of interfaces a couple of methods in a couple of classes that's about it it doesn't have to do anything real just create something and test it out make sure you understand how mocking works in a test environment first don't try and put us in real production code do that three to five times for mocking I recommend five times because it is a little bit more complicated a little more tricky than you know if statements okay it's a little more higher level than that so make five projects just try them out try a little different thing every time start the simplest thing and kind of work your way out and that hands-on experience of doing that those five times will help make it easier you'll start to get the feel for the different pieces you put in because what's gonna happen is the first time you prime forget something and go wait wait wait what was happening you go back to the video and watch it again and go oh okay I just said get loose instead of get strict or okay that's how I did return or how are we turns data and so the first time you're gonna do that meet a second time grab forget something else by a third time fourth time you're probably getting the pattern you don't have to watch the video in order to to build something out but a fifth time it's just like why am I doing this it's so sick you know I got it that's good get to that point if it takes you 10 projects do it this is how you learn but actually doing it don't just watch actually do it okay sorry get off my soapbox now I hope you enjoy the video if you have any questions post them down below in the comments I'll try to get to as many as possible it's kind of hard to answer really technical questions in the comments don't commit get it my best shot and if you do have a technical question try and make it try make sure that you've checked our video first that's one let's think that that's pretty common is you missed something was in the video so just check the video up first before you post that question okay thanks so much if you like this hit the like button I appreciate it now see you next week [Music] you [Music]
Info
Channel: IAmTimCorey
Views: 177,775
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# training, C# tutorial, unit testing, c# unit testing, c# unit testing mock, c# unit testing with xunit, c# unit testing dependency injection, c# unit testing example, mocking, mocking in C#, moq, sqlite, s.o.l.i.d, s.o.l.i.d principles, s.o.l.i.d principles c#, s.o.l.i.d design principles, dependency inversion principle, c# example, unit testing tutorial, .net unit testing, mock object, tim corey c#
Id: DwbYxP-etMY
Channel Id: undefined
Length: 62min 33sec (3753 seconds)
Published: Mon Aug 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.