views:

62

answers:

1

Hi

I am still confused of when I have to make a wrapper and interface to fake my tests.

Like in a book I am reading about MVC the author uses the Moq framework.

So the author first makes a IFormAuthentication interface.

Writes some methods there and then makes a WrapperClass that implements these methods and then writes the actual code for the methods(ie signout).

So then in Moq he just uses the interface. So this makes sense to me but correct me if I am wrong.

He is doing this because he wants moq to make a fake mockup by using the interface? Then in MVC application he has it step up so that if the interface is null it will then make a new wrapper class.

So I am guessing this is so when it time to actually run it uses the wrappers that actually contains the real methods so that the application will work as it should.

So hopefully I got that right.

Now he goes on to do the Membership ones and he is says something like "look how many methods I would have to implement with an interface(I am also guessing he would make a wrapper too)".

Instead we will get Moq to do it and then he passes to Moq MembershipProvider and it creates all this stuff.

So my questions is how did he know? Like how is it on one hand you can't do FormsAuthentication methods this way but you can do the MembershipProvider?

I don't even think you can do Like just Membership it has to be MembershipProvider.

So where do I get this information? Like I want to do my SMTP(MailMessage) and I want to know if I have to write an interface and then a wrapper or can I do the same thing like MembershipProvider?

I am not sure I don't know how to tell.

Thanks

A: 

The reason why the author didn't extract an interface for MembershipProvider is because it is already an abstract class, so it serves excellently as a Test Double already.

In other words: Extracting an interface is necessary only if you want to abstract away a class that is outside of your control. In that case, you can then make an Adapter that wraps the real implementation, while still providing the ability to replace the real implementation with a Test Double.

When it comes to Abstractions, interfaces and base classes with virtual members are conceptually equivalent.

You can read more about Test Doubles in the excellent xUnit Test Patterns.

Mark Seemann