Nunit测试中在哪里实例化接口

发布于 2024-09-29 19:11:59 字数 1522 浏览 3 评论 0原文

我认为我错过了一些非常简单的东西,所以我提前道歉。我正在尝试使用 Nunit 测试接口。该接口由派生自基类的类实现,我使用 Castle Windsor 进行 IOC。我根本不知道在派生测试类中将接口分配到哪里。

这是基本测试类,

[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);
    }
}

这是派生测试类,

[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);
    }
}

我已在温莎容器中注册了存储库,并且在我的所有控制器中运行良好,只是不知道如何测试接口。我唯一的解决方案是在设置方法中为接口分配一个具体的实现,但我想知道是否应该使用 DI 以某种方式处理该问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-10-06 19:11:59

询问,您将收到:)

您需要在测试中引用您的容器,并且需要调用 .Resolve() 我相信这就是 Castle 所说的方法,但我可能是错的。

为了在测试中获得对容器的引用,您需要在某个时候创建​​容器。我并不是真正的 Castle 专家,但查看此页面上的代码看起来像是一个关于如何新建容器并解决依赖项的非常简单的示例

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文