gpt4 book ai didi

c# - Nunit 测试给出结果 OneTimeSetUp : No suitable constructor was found

转载 作者:太空狗 更新时间:2023-10-29 20:04:19 46 4
gpt4 key购买 nike

我遇到一个问题,NUnit 告诉我:“找不到合适的构造函数”。是什么原因造成的?我还收到另一条消息:“Exception doesn't have a stacktrace”。这两条消息只是一遍又一遍地重复。这是我的代码

[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 中都有效。

但是,为了让 NUnit 使用该构造函数,您必须给它一个要应用的参数,而您还没有这样做。您可以通过指定

[TestFixture(someArgument)]

您可能打算通过在 TestFixtureSetUp 中为 stubIContext 赋值来做类似的事情。但是,这行不通,原因有二:

  1. 它没有提供给构造函数,而这正是您的夹具需要它的地方。

  2. 无论如何,对象的构建发生在调用设置方法之前。

有几种方法可以在夹具实例化之前创建 stub ,特别是在 NUnit v3 中。但是,我实际上不明白为什么您需要参数化此夹具,因为您无论如何都在使用 stub 。

除非您有其他参数化需求(示例中未显示),否则我会在设置中简单地创建 stub 。我的偏好是使用 SetUp 而不是 TestFixtureSetUp。创建 stub 并不昂贵,因此似乎没有理由节约。但是,如果摘录中没有出现原因,TestFixtureSetUp 也可以正常工作。

关于c# - Nunit 测试给出结果 OneTimeSetUp : No suitable constructor was found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722194/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com