Nunit测试给出结果OneTimeSetUp:未找到合适的构造函数 [英] Nunit test gives result OneTimeSetUp: No suitable constructor was found

查看:367
本文介绍了Nunit测试给出结果OneTimeSetUp:未找到合适的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,NUnit告诉我:找不到合适的构造函数。是什么原因造成的?我还收到另一条消息:异常没有堆栈跟踪。这两个消息只是一遍又一遍地重复。这是我的代码

I have an issue where NUnit is telling me: "No suitable constructor was found". What causes this? I also get another message: "Exception doesn't have a stacktrace". Both messages just repeat over and over again. Here's my code

[TestFixture]
public class SecurityServiceTests
{
    private IContext stubIContext;
    private ISecurityService securityService;
    private IWindsorContainer windsorContainer;

    public SecurityServiceTests(IContext stubIContext)
    {
        this.stubIContext= stubIContext;
    }

    [TestFixtureSetUp]
    public void TestSetup()
    {
        //Mocks the database context
        stubIContext= MockRepository.GenerateStub<IContext>();
        var returnedList = new List<string>();
        stubIContext.Stub(a => a.GetUserSecurities(null)).IgnoreArguments().Return(returnedList);

        securityService = new SecurityService(windsorContainer);

    }

    [Test]
    public void ControllerShouldGetUserGroupForCurrentUsers()
    {
        //Act
        var action = securityService.CurrentUserFeatureList;

        //Assert
        Assert.IsNotNull(action);
    }


}


推荐答案

您正在尝试创建参数化的固定装置,因此您有一个使用单个参数的构造函数。与上面的评论相反,这在NUnit V2和V3中均有效。

You are trying to create a parameterized fixture, so you have a constructor taking a single argument. Contrary to the comment above, this is valid in both NUnit V2 and V3.

但是,为了让NUnit使用该构造函数,您必须为其提供一个要应用的参数,而您尚未这样做。您可以通过指定

However, in order for NUnit to use that constructor, you have to give it an argument to be applied and you have not done so. You would do this by specifying

[TestFixture(someArgument)]

可能您打算通过在TestFixtureSetUp中为stubIContext分配一个值来执行类似的操作。但是,这不能工作有两个原因:

Probably, you are intending to do something like that by assigning a value to stubIContext in the TestFixtureSetUp. However, that can't work for two reasons:


  1. 它没有提供给构造函数,这就是您的灯具需要的地方

  1. It's not being supplied to the constructor and that's where your fixture needs it.

无论如何,对象的构造是在调用该设置方法之前进行的。

Anyway, construction of the object takes place before that setup method is called.

有几种方法可以在实例化夹具之前获取创建的存根,特别是在NUnit v3中。但是,实际上我不明白为什么要对这个灯具进行参数化,因为无论如何您都在使用存根。

There are several ways to get the stub created before the fixture is instantiated, particularly in NUnit v3. However, I don't actually see why you need this fixture to be parameterized, since you are using a stub anyway.

除非您还有其他一些参数化需求,如示例中所示,我只需在设置中创建存根即可。我的偏好是使用SetUp而不是TestFixtureSetUp。创建存根并不昂贵,因此似乎没有理由进行节省。但是,如果某些原因未在摘录中看到,则TestFixtureSetUp也可以正常工作。

Unless you have some other need for parameterization, not shown in the example, I would simply create the stub in the setup. My preference would be to use SetUp rather than TestFixtureSetUp. Creating stubs is not expensive, so there seems to be no reason to economize. However, if there are reasons not seen in the excerpt, TestFixtureSetUp can work as well.

这篇关于Nunit测试给出结果OneTimeSetUp:未找到合适的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆