First, you won't find many resources in VB. The best option is to look at the C# resources and then just look at this comparison chart to see what most of it is doing. Ignore a lot of the fancy syntax and just work on instantiating and filling in the parameters with the correct predicates (Function(x)
/Sub(x)
instead of x => ...
).
Is my understanding of how Microsoft Fakes and Moq work even correct?
Close. From the wording of your question, it sounds like you think Moq or Fakes will mock an actual database. It will not make you a fake database (SQL/Raven/etc.). What it does is create you a mocked object of what you need.
So if you have a Repository
class, you can set it up to give a proxy back. It may look like your type and work as your type, but it is a "fake" or "mock" of the type. This is why you can force specific values to return. This means if you have a PersonRepository
with a method of Find(personId) As Person
, you can have it return a new instance (or mock) of Person
with the data you need to for the test. You specify what you need. It won't connect to database and the code that would normally be in Find
won't run because you didn't really get a true instance of PersonRepository
, but of course a mock based on what you told it to set up.
Are Microsoft Fakes and Moq 2 mocking frameworks with different behavior or do they have different goals?
They have close to the same goals, but work completely differently. There are pros and cons for both. One of the main reasons one would lean more towards Microsoft Fakes are Shims. Shims allow the mocking of static
and Shared
. As far as I know, only Fakes and other 3rd party frameworks that cost money have this feature.
The biggest difference between Fakes and Moq is that Fakes uses code generation to allow stubbing and shimming. You will end up with xAssembly.Fakes.dll in your unit test projects for whatever DLL you need to "fake".
I highly recommend looking at this answer for good information on Fakes.
Are they compatible? Is it possible to use both, and does it have any value?
I don't see why not. They are both going to give you back fake objects that will look like your original object. There's no reason you can't create one object in Fakes and one in Moq. A mocked IRepository
is still an IRepository
-- doesn't matter which one created it. You can play with both at the same time to see which one you prefer.
Fakes is really good and handy when working with old code that you cannot rewrite, but need to test. Being able to shim statics (Shared
for VB) is a great feature. However, I do prefer Moq for normal unit test writing where statics do not come into play (because they shouldn't).