chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Suppose I have an abstract class that I need to mock to use in the test of another class:

public abstract class MyAbstractClass
{
    /...
}


In the particular case of this test, I need the class to implement the IXmlSerializable interface. It would be nice to have something like this:

RecordManager.CreateMockObject<MyAbstractClass>(Constructor.Mocked, StrictFlags.AllMethods, new InterfaceImplementation[] { new InterfaceImplementation(typeof(IXmlSerializable), InterfaceImplementationType.Implicit) }, ...);


Since the number of parameters for CreateMockObject is starting to grow, a settings class could be introduced:

public class CreateMockObjectSettings
{
    Constructor Constructor { get; set; }
    StrictFlags StrictFlags { get; set; }
    object[] Parameters { get; set; }
    InterfaceImplementation[] Interfaces { get; set; }
    // ...
}


With the new C# 3.0 object creation syntax, the legibility is good:

RecordManager.CreateMockObject<MyAbstractClass>(new CreateMockObjectSettings { Constructor = Constructor.Mocked, StrictFlags = StrictFlags.AllMethods, Interfaces = new InterfaceImplementation[] { new InterfaceImplementation(typeof(IXmlSerializable), InterfaceImplementationType.Implicit) } });
asked by paulo.morgado (11k points)

8 Answers

0 votes
Hi Paulo,

Thanks for the suggestion. We'll add it to our feature wish list, and see how we fit it into a future version. As always, I encourage people to sound out if they would like a feature like that as well.
answered by gilz (14.5k points)
0 votes
I like it, as long as the default no-parameter behavior is kept.
answered by tillig (6.7k points)
0 votes
Hi Paulo,

If I understand you correctly you wish that the mock object created to implement a given interface which the original abstract class did not implement.

:?: If this is true, can you explain how you would use the created instance in you test?
answered by lior (13.2k points)
0 votes
Hi Lior,

Let's go crazy here and say any type that's not a sealed class. I might want an object that implements several interfaces no matter what class it is.
answered by paulo.morgado (11k points)
0 votes
Hi,
Sorry I didnt understand the answer. :?
Lets say that you have this API and you can create anything you like.
Can you show me how will such a created object be used later in a test?
answered by lior (13.2k points)
0 votes
It's more about what I want to test than how I code the test.

Specially in ASP.NET controls, you see classes that implement multiple interfaces and, in some contexts, it's what interfaces the object implements that matters, not the fact that it's a control.

Another example is IIdentity implementations. Imagine you have a persistence system for identities is optimized for identities implementing ISerializable. I don't want to create another type just to type just for the sake of a particular test.

Serialization is the most immediate example I can think of for adding custom interface implementation (and attributes ^) that I can think of.
answered by paulo.morgado (11k points)
0 votes
Ok I see now. :D

what you actually need is something like:
MockManager.CreateMockObjectImplementing(Type[] ListOfInterfaces);


:idea: For now what you can do is locate the actual concrete class that is passed to those methods in "real" execution and just mock that type.
answered by lior (13.2k points)
0 votes
Almost!

The problem with implementing interfaces is that it may be an explicit or implicit implementation, thus my suggestion:

new InterfaceImplementation(typeof(IXmlSerializable), InterfaceImplementationType.Implicit)


or:

new InterfaceImplementation(typeof(IXmlSerializable), InterfaceImplementationType.Explicit)
answered by paulo.morgado (11k points)
...