tags:

views:

52

answers:

2

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());
A: 

From the interfaces itself you cannot raise events, as interfaces cannot contain code, only contracts. If you want to communicate between objects that implement IOne and ITwo, the best way is, indeed, to add an event sink and register to that event from either of the implementations.

But: you cannot do this automagically through interfaces alone.

Edit

You meanwhile edited your question. What is Mock<T> for you? You cannot cast from ITwo to Mock<T>. The reverse might be possible if Mock<T> inherits from ITwo.

Note: Perhaps an example with words other than One and Two might help us understand what you really after?

Code example

Answering just "How can I raise an event on ITwo when I only have a reference to IOne or Mock", hopefully I understand a bit what you're after:

interface IOne 
{
    void RaiseEventITwo();
    ITwo Two { get; set;}   
}

// note: left out some details
class One : IOne
{
    public One() { this.Two = new Two(); }    // Two implements ITwo
    public void RaiseEventITwo()
    {
        this.Two.OnLoadPage();   // i.e.: some event of ITwo is OnLoadPage
    }
}

// somewhere where you actually have the reference you speak off
IOne one = new One();   // class One implements IOne
one.RaiseEventITwo();

// using the Mock<T> class:
Mock<IOne> mockOne = new Mock<IOne>();
mockOne.GetIOne().RaiseEventITwo();      // GetIOne needs to be implemented in 
                                         // Mock<T> or as extension for Mock<T>
Abel
A: 

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 from ITwo to Mock<ITwo>?")

It's pretty easy: there's a Mock.Get method that finds the Mock<T> for a T (as long as that object came from Moq).

Mock<ITwo> mockTwo = Mock.Get(two);

Details can be found in the Moq Getting started guide:

http://code.google.com/p/moq/wiki/QuickStart#Advanced_Features

Royston Shufflebotham
I don't know how I missed that. That's exactly what I need. Thanks!
Colin Anderson