What is Mocking ?

Aref K.
A free video tutorial from Aref K.
Chief Technology Officer
4.2 instructor rating • 10 courses • 40,288 students

Learn more from the full course

Unit Testing .Net Core Applications with xUnit .net & MOQ

Mocking Techniques, Test Driven Development (TDD), and Unit Testing Legacy Code with xUnit.NET + Interview Questions

03:30:49 of on-demand video • Updated May 2019

  • Learn all the features and capabilities of xUnit .net
  • Learn writing unit tests using the Mocking technique and xUnit
  • Learn how to write unit tests for legacy (old) applications
  • Learn Test Driven Development (TDD) with xUnit
  • Learn how to measure test coverage
  • Take the interview questions and get ready for your next interiew
English [Auto] So so far we learned how to use x unit and we said that X you need is a tool or framework for writing unit tests. But I need to now just mention why we call these kind of tests unit tests and what is mocking and why do we do mocking and why do we write unit tests. So if we think of a standard three tier or three layer application you will have your user interface or your presentation layer or tier and then you will have your business layer or tier depending on whether it's physical or not. And then you have your data access layer which can be just a class that writes to database and reads data from a database. Translating this to a C doesn't MVC website you have for example some razor views CSX Gemma files and you imagine that you have lucky form on the RAZR view. When user types in the data and clicks on the submit button. All that data will be passed through controller and controller will pass the data to your business class and then your business class will do the business logic does all the checks and validations and then it'll pass the data to a another class I call it repository because if you implement the repository pattern you probably will have repositories and then your repository will drive the data to a real database for example to a sequel server or my sequel database and then when you want to read the data your repository will read the data from a real database again to overcome the problem of testing our business class what we can do in here is that if we want to only test what's happening in our business class without having to really deal with a real database what we can do is that we can implement an interface. So we introduce an interface and our repository class will implement that interface and then the business class will only deal with the interface. So we kind of use a layer of abstraction to decouple our business class from our repository class by doing so we can isolate that business class and only focus on that and test that piece of code without having to write anything to database. So what we will do is that we will create a test class for example we can call it business class test and then inside our test method or test methods we will create an instance of the real business class. So that's a real instance. But then because our business class only works with our repository and has no idea what is behind that repository we can easily not use the real repository and just have a class that implements repository. But it really does nothing. That's why I call it empty or fake repository. So it has the same methods that the real repository has. But then those methods imagine that are empty and they don't do anything. And because they don't do anything we don't deal with a real database and therefore there is no hard dependency in here. So we can now go to visual studio and I will show you an example that how we can use this technique to test our business class or any class. For example you can test your controller without having to deal with the real business class or with the repository. You can isolate any class any part of your application with this technique and just write the unit tests for it. That's why we call this unit test because you only test one unit or one piece of your application without having to deal with any other parts of the application. So we go to visual listed and I will show you how we can do these using the mock framework.