locked
Mocked objects or Mocking interfaces RRS feed

  • Question

  • Hi,

    I have used moq for quite time and i am new to MSTest.

    now i am not able understand that how MSTest provide option to mock the interfaces or abstract classes.

    even i have come questions:

    MSTest is providing u to test the code and it doesn't include any mocking framework?

    then MSTest alone won't stand for Testing Framework?

    Thanks,

    Wednesday, July 30, 2014 11:38 AM

Answers

  • MSTest is a testing framework it self.

    Moq is not a testing framework, Moq and Microsoft Fakes are mocking libraries. I recomend you to read Martin Fowler blog post "Mocks aren't stubs" http://martinfowler.com/articles/mocksArentStubs.html

    Basically, with a testing framework (like MSTest) you got utilities to run and check if your code does what is suposed to do: asserts, test initialization, etc.

    Some times you will need to isolate some parts of the code you want to test, i.e. accesing a database, file system, that you don't want to test. For example, if you are developing a shopping cart and you whan to test that when you add an product to your shopping cart, the amount of products in the shopping cart must be incremented in 1 product. it is possible that this operation uses a database, or a cache, or some artifact to store the data. So, as you do not want to test the database access, cache,etc. , you will find usefull to fake that behavior to get a quick and isolated test. (Among best practices in software design by separating responsabilities to get a uncoupled and cohesive design)

    You can achieve that without using any mocking object as Uncle Bob describes here http://blog.8thlight.com/uncle-bob/2014/05/10/WhenToMock.html

    However, a mocking library is very usefull because it offers tools to create those "fakes" very quickly and configure only those parts of the mock you will need in your tests, without having to write down all fake classes by yourself.

    Hope this helps and happy testing!


    Sólo soy un androide de protocolo y no valgo para contar historias. http://speakingin.net


    • Edited by JuanLao Thursday, July 31, 2014 5:38 PM
    • Marked as answer by Amanda Zhu Wednesday, August 6, 2014 1:57 AM
    Thursday, July 31, 2014 5:35 PM
  • Hi,

    Now Microsoft provides Fakes Framework to ‘mock’ the interface or abstract classes under unit test from VS2012. Fakes Framework help you isolate the code you are testing by replacing other parts of the application with stubs or shims.

    For more information, please see: Isolating Code Under Test with Microsoft Fakes

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Amanda Zhu Monday, August 4, 2014 2:08 AM
    • Marked as answer by Amanda Zhu Wednesday, August 6, 2014 1:57 AM
    Thursday, July 31, 2014 3:23 AM

All replies

  • Hi,

    Now Microsoft provides Fakes Framework to ‘mock’ the interface or abstract classes under unit test from VS2012. Fakes Framework help you isolate the code you are testing by replacing other parts of the application with stubs or shims.

    For more information, please see: Isolating Code Under Test with Microsoft Fakes

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Amanda Zhu Monday, August 4, 2014 2:08 AM
    • Marked as answer by Amanda Zhu Wednesday, August 6, 2014 1:57 AM
    Thursday, July 31, 2014 3:23 AM
  • MSTest is a testing framework it self.

    Moq is not a testing framework, Moq and Microsoft Fakes are mocking libraries. I recomend you to read Martin Fowler blog post "Mocks aren't stubs" http://martinfowler.com/articles/mocksArentStubs.html

    Basically, with a testing framework (like MSTest) you got utilities to run and check if your code does what is suposed to do: asserts, test initialization, etc.

    Some times you will need to isolate some parts of the code you want to test, i.e. accesing a database, file system, that you don't want to test. For example, if you are developing a shopping cart and you whan to test that when you add an product to your shopping cart, the amount of products in the shopping cart must be incremented in 1 product. it is possible that this operation uses a database, or a cache, or some artifact to store the data. So, as you do not want to test the database access, cache,etc. , you will find usefull to fake that behavior to get a quick and isolated test. (Among best practices in software design by separating responsabilities to get a uncoupled and cohesive design)

    You can achieve that without using any mocking object as Uncle Bob describes here http://blog.8thlight.com/uncle-bob/2014/05/10/WhenToMock.html

    However, a mocking library is very usefull because it offers tools to create those "fakes" very quickly and configure only those parts of the mock you will need in your tests, without having to write down all fake classes by yourself.

    Hope this helps and happy testing!


    Sólo soy un androide de protocolo y no valgo para contar historias. http://speakingin.net


    • Edited by JuanLao Thursday, July 31, 2014 5:38 PM
    • Marked as answer by Amanda Zhu Wednesday, August 6, 2014 1:57 AM
    Thursday, July 31, 2014 5:35 PM