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

preview_player
Показать описание
Unit tests are great, but how do you test your data access? You sure don't want to actually delete records when you are testing the delete method, yet you definitely want to be sure the method is configured properly. This is the type of problem that stops a lot of developers from creating unit tests. In this video, I am going to show you how to overcome these obstacles in order to write tests that handle working with external dependencies like databases.

Sponsors:
Tuhafeni Angula (Patreon Subscriber)

0:00 - Intro
1:05 - Getting started: demo application code and setup
10:20 - Problems with testing methods that manipulates with external stuff (database edits, e-mail senders etc)
12:39 - Mocking explained
15:44 - Writing tests with mocking
18:05 - Adding references for testing with MOCK
20:51 - Using statement
22:12 - AutoMock.GetLoose() vs AutoMock.GetStrict();
23:30 - Using AutoMock: Writing the Unit Test with mocking
35:06 - Refactoring method that's under a UnitTest
36:03 - Checking data: comparing two objects
40:19 - Testing method that returns void
48:16 - Testing SQl call modification
52:47 - Recap
53:30 - Advice for beginners
55:15 - What not to test and why
58:24 - Summary
59:27 - Learning steps
1:01:42 - Concluding remarks
Рекомендации по теме
Комментарии
Автор

Have use Moq testing today, since we're starting to implement this framework within our product. Thanks to this video, it didn't take me long to create unit tests.

YOUTUBE_IS_WOKE
Автор

Hey Tim,
Great video as usual. There's absolutely no reason you should be getting any thumbs down. There are those who know how to code really well but don't know how to teach, but you can do both hands down.
Thank you for giving back to the community.

buddyrowe
Автор

I really like how your videos are segmented so that I can jump to a particular area by hovering over the progress bar.

stratman
Автор

Thanks for making a complicated subject easy to understand. You have a real talent for teaching!

steveteece
Автор

Tim, I really admire you the way you explain the things. I remember ignoring this lesson in 2018, but today I came back to the very same tutorial. Happy to learn from you Tim Thanks

jidt
Автор

0:00 - Intro
1:05 - Getting started: demo application code and setup
10:20 - Problems with testing methods that manipulates with external stuff (database edits, e-mail senders etc)
12:39 - Mocking explained
15:44 - Writing tests with mocking
18:05 - Adding references for testing with MOCK
20:51 - Using statement
22:12 - AutoMock.GetLoose() vs AutoMock.GetStrict();
23:30 - Using AutoMock: Writing the Unit Test with mocking
35:06 - Refactoring method that's under a UnitTest
36:03 - Checking data: comparing two objects
40:19 - Testing method that returns void
48:16 - Testing SQl call modification
52:47 - Recap
53:30 - Advice for beginners
55:15 - What not to test and why
58:24 - Summary
59:27 - Learning steps
1:01:42 - Concluding remarks

RalfsBalodis
Автор

It had been a while since I finished watching a video with the excitement of having just learned something new and empowering. Thank you.

giovanni-cxfb
Автор

This is the kind of video that I'll never be able to finish watching. Kept falling asleep, then rewind back to watch again but fall asleep again. I've rewinded about four times this afternoon. 😂

conaxlearn
Автор

even thou I use Nunit for testing, but this video helped me quite a bit. Thanks a lot. Yours are good, cause you not only show HOW - but you tell WHY. Pretty rare on youtube I say. Thanks again!

ivanyablonskyi
Автор

Damn you Tim You videos are amazing and so addictive, it's 4 AM here and i have work tomorrow. Thanks a lot for sharing you knowledge.

pylvr
Автор

I've watched 2 or 3 other training videos on this topic, and yours clears it up for me. Thanks!

mugaland
Автор

Again you are coming in clutch! Another great video! I still find it quite hard to know how to use mocking in my tests I am supposed to write, but this definetly gave me a better idea about what mocking is, and now I mind be able to read the other tests and actually understand what is going on! Thank you!

thobiaslarsen
Автор

Thank you so much for all your videos I can't stress enough the amount of gratitude I, and I'm sure the rest of us, have for you, so truly thank you.

iyadisleem
Автор

Thank you so much for this clear and concise explanation..
I tried learning mocking from a book a year back but never understood it that time.. i thought I should give it a try one more time..
Now I Finally know what mocking is ..
Thanks again..

shaikhabdulgaffar
Автор

Beautifully explained with calm voice. So good!

sumits
Автор

I've been searching for a while a good content for tests in Microsoft environment and now I found a good explanation. Finally I understand the reason and how to test void and CRUD methods.

TiagoCrizanto
Автор

Thanks for showing me a whole new world, shining, shimmering, splendid...

xianyu
Автор

You the man TIm! My go 2 for C# learning. Cheers

sahawndada
Автор

Hello Tim. Another good video. After digging, I found that Moq appears to be the industry standard for mock-ing (based on the nuget download count). So, even though the video is nearly 2 years old, the framework is still good to get familiar with.

queenstownswords
Автор

Tim, this was a good video.  I was a bit hesitant about mock frameworks as I am a firm believer in writing all code when it comes to unit test.  That's the way I was taught in the 90's.  I watched your video several times and then it clicked it my thought process on how you used moq framework and more importantly why.  I had to think back on how I  wrote all the extra code to test to realize how this can cut down on writing so much of it before being able to find bugs.  Thanks for a great video, I do appreciate this and look forward to watching more.

cedricrogers