Say I have an interface that has as property another interface. Something like:
interface IOne
{
ITwo Two { get; set;}
}
And I create a mock for ITwo and then create a mock for IOne using Mock<ITwo>.Object. How can I raise an event on ITwo when I only have a reference to IOne or Mock<IOne>? I need a reference for Mock<ITwo>, but how can I cast from ITwo to Mock<ITwo>? I hope it’s clear 🙂
Thank you.
edit:
I hope this exampler is clearer:
interface IHuman
{
void Sneeze();
INose Nose { get; set;}
}
interface INose
{
event EventHandler Irritated;
}
class ToBeTested
{
private IHuman _human;
public ToBeTested(IHuman human)
{
_human = human;
_human.Nose.Irritated += NoseIrritated;
}
void NoseIrritated(object sender, EventArgs e)
{
_human.Sneeze();
}
}
I want to be able to access Mock from Mock. So I can do somethiong like:
noseMock.Raise(nose => nose.Irritated += null);
humanMock.Verify(human => human.Sneeze());
I’m still not entirely sure what you’re asking for, but it looks like you’re trying to work out how to get from a mocked object to the
Mock<T>wrapper that produced it? (“how can I cast fromITwotoMock<ITwo>?”)It’s pretty easy: there’s a
Mock.Getmethod that finds theMock<T>for a T (as long as that object came from Moq).Details can be found in the Moq Getting started guide:
http://code.google.com/p/moq/wiki/QuickStart#Advanced_Features