views:

37

answers:

1

I think theres something really simple I'm missing so I apologize in advance. I'm trying to test an interface with Nunit. The interface is implemented by a class derived from a base class and I'm using Castle Windsor for IOC. I simply dont know where to assign the interface in the derived test class.

Here is the base test class

[TestFixture]
public class BaseTest
{
    protected ISession session;
    [SetUp]
    public void setup() {
        NHibernateConfig.Init(
            MsSqlConfiguration.MsSql2008.ConnectionString(
            builder =>
            builder.Server("localhost")
            .Database("Db_test")
            .TrustedConnection()),
            RebuildDatabase());
        session = NHibernateConfig.CreateAndOpenSession();
    }
    [Test]
    public void Shoud_Test_Connection(){
        // testing connection via setup fixture
    }
    [TearDown]
    public void TearDown(){
        if (session != null)
            session.Dispose();
    }
    private Action<Configuration> RebuildDatabase() {
        return config => new SchemaExport(config).Create(false, true);
    }
}

here is the derived test class

[TestFixture]
public class RepositoryTest : BaseTest
{
    IRepository repository;
    [SetUp]
    public void Setup(){
      // I think the interface should get assigned
      // in here somehow....
    }
    [Test]
    public void Should_Create_And_Read(){
        var post = CreatePost();
        var actual = (IList) repository.GetAll();
        Assert.Contains(post, actual);
        Assert.AreEqual(1, actual.Count);
    }
}

I have the repository registered in my windsor container and works fine in all my controllers, just cant figure out how to test the interface. My only solution is to assign the interface a concrete implementation in the setup method but I'm wondering if I'm suppose to use DI to handle that somehow.

+1  A: 

Ask and you shall receive :)

You need a reference to your container in your test and you need to call .Resolve() I believe that is what Castle calls their method but I could be wrong.

In order to get a reference to your container in your test you need to create your container at some point. I am not really a Castle expert but check out the code on this page looks like a pretty simple example on how to new up a container and resolve a dependency

http://stw.castleproject.org/Windsor.MainPage.ashx

Foovanadil