6 Replies - 1080 Views - Last Post: 11 December 2015 - 10:06 AM

#1 PratikRatanpara  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-December 15

How to mock .NET Framework

Posted 08 December 2015 - 05:39 AM

Hello,
I am writing a unit test case for my application.In my application, I am using SQLite datebase. I want to mock .net framework methods like Convert.ToString() etc & File.Exist(strPath), etc.
Is This A Good Question/Topic? 0
  • +

Replies To: How to mock .NET Framework

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: How to mock .NET Framework

Posted 09 December 2015 - 09:18 AM

I split off this question from the other thread about unit testing internal/private methods because this is about mocking the public .NET Framework methods.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to mock .NET Framework

Posted 09 December 2015 - 09:23 AM

Quote

How To Mock .NET Framework


You point at it and laugh.
Was This Post Helpful? 1
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13566
  • View blog
  • Posts: 54,125
  • Joined: 12-June 08

Re: How to mock .NET Framework

Posted 09 December 2015 - 09:29 AM

Damn it! Someone beat me to it!


Enjoy your rim shot.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: How to mock .NET Framework

Posted 09 December 2015 - 10:46 AM

I'm of multiple minds about this question...

In my opinion, if you are testing against your SQLite database, then you are doing integration testing, not unit testing.

If you need to mock some .NET Framework functions, you really should encapsulate them in a lightweight interface or use the Func's and/or Action's. This makes it clear that you have some dependencies an particular methods that may influence the results of the code being tested.

On the other hand, newer versions of VS provide automated ways of building fakes -- stubs and shims. These are not true mock objects in the original context of unit testing, but they tend to be functionally the same.

I can see how going with the fakes approach tends to provide less code clutter (e.g. no lightweight interfaces or extra Func's or Action's), but as of now I tend to lean on the side of making the intent of code explicit at the cost of that clutter. I may change my stance after I've tackled a huge project using fakes instead of mocks.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: How to mock .NET Framework

Posted 10 December 2015 - 07:04 AM

After some more thinking time on this while stuck in traffic, I'm coming to the conclusion that using fakes is the antithesis of Test Driven Development. Think about it: If you are supposed to write the test first before writing the code to be tested, then you should not know how the code is implemented, and therefore you wouldn't know ahead of time what needs to be shimmed.

And even worse, now you have very tight coupling between the unit test and the code. You can't refactor the code with full freedom without changing the test. To me, this feels like it goes against the idea of having unit tests as a safety net while you are refactoring.
Was This Post Helpful? 0
  • +
  • -

#7 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 446
  • View blog
  • Posts: 2,179
  • Joined: 07-April 08

Re: How to mock .NET Framework

Posted 11 December 2015 - 10:06 AM

I tend the sit in the camp that fakes things, especially database interaction. I use FakeItEasy as well as Mock Repositories to run my tests against so that I have a bit more control over everything. I know that if I'm testing against my mock repository that the data wont change because someone else went in and made a change. I can rely on the data more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1