Skip to content
fawei edited this page Jan 9, 2013 · 4 revisions

###Simple Example This example uses the MoqMockingKernel. Please adapt accordingly if you use another mocking framework.

public interface IBar
{
    string GetContent();
}
public interface IFoo
{
    void DoStuff();
}

public class MyFoo : IFoo
{
    private readonly IBar _bar;
    public MyFoo(IBar bar)
    {
        _bar = bar;
    }

    public void DoStuff()
    {
        Console.WriteLine(_bar.GetContent());
    }
}


[TestFixture]
public class MyTests
{
    private readonly MoqMockingKernel kernel;
    
    public MyTests()
    {
        this.kernel = new MoqMockingKernel();
    }

    [SetUp]
    public void SetUp()
    {
        this.kernel.Reset();
        this.kernel.Bind<IFoo>().To<MyFoo>();
    }

    [Test]
    public void Test1()
    {
        //setup the mock
        var barMock = this.kernel.GetMock<IBar>();
        barMock.Setup(mock => mock.GetContent()).Returns("mocked Content");

        var foo = this.kernel.Get<IFoo>(); // this will inject the mocked IBar into our normal MyFoo implementation
        foo.DoStuff();  // this will print "mocked Content", because the mocked object is called

        barMock.VerifyAll();
    }
}
Clone this wiki locally