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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I need to mock an interface but I need the mocked instance to be serializable. Can this be done?

(On a broader usage, it would be nice to apply any set of attributes to a mocked instance type.)
asked by paulo.morgado (11k points)

7 Answers

0 votes
Hi Paulo,

When using MockObject API to mock interfaces we actually create a new dynamic class which implement that interface. If my memory serves me right we don't copy the attributes decorating the interface.
However, since most likely attribute on interfaces are intended to be inherited the mocked instance should be in this case serializable as well.

can you post the exact interface code so I can verify this?
answered by lior (13.2k points)
0 votes
(The particular case of the SerializableAttribute is different because it can't be applied to interfaces.)

I wouldn't do that because you expect "normal" inheritance to be applied.

But it would be nice (and not hard to do) to have an array of Attribute instances passed into the mocking method and have those attributes applied to the generated mock type.

You could go a bit further and have another parameter for an array of interfaces to implement.

RecorderManager.CreateMockedObject(Type, Constructor, StrictFlags, Attribute[], Type[], params object[]);
answered by paulo.morgado (11k points)
0 votes
adding this API is one possibility.
However, I believe it wont be too hard to handle this when we create the dynamic class. That way the user wont need to worry abouth this at all. the created instance will behave as expected.

Let me raise it up here and see what people think about this.
In any case ive added this request to our future list.
answered by lior (13.2k points)
0 votes
I don't think it would be hard, also. But I wouldn't mess with it in this way. Just let the normal inheritance take place and allow the user to provide custom attributes (and interfaces, while you are at it).
answered by paulo.morgado (11k points)
0 votes
Any new developments on this topic? I'm currently looking to mock a custom interface that, by definition, requires its implementor to also implement ISerializable. Since I can't attribute my custom interface with [Serializable], I'm unable to mock it.

I can work around this by creating a temporary class that implements my custom interface, attribute it with [Serializable], and use an instance of it in my tests, but it's messy. A simple line of code to fake an object would be far more preferable.
answered by navdan (720 points)
0 votes
Hi,

Not sure I understand the scenario correctly :)
If the interface you want to fake looks like this:
public interface IToFake : ISerializable
{        
}

You can still fake it and the fake will have dummy implementation of ISerializable.
Can you please post here a code example of the scenario?
answered by ohad (35.4k points)
0 votes
I should have been more specific. I can mock the object without any problem. However, I can't actually serialize it via .NET binary serialization.[/code]
answered by navdan (720 points)
...