Nunit test gives result OneTimeSetUp: No suitable constructor was found

16,840

Solution 1

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.

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)]

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. It's not being supplied to the constructor and that's where your fixture needs it.

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

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.

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.

Solution 2

Your SecurityServiceTests class needs to have a default constructor to be used as a TextFixture.

From the docs on TextFixture:

There are a few restrictions on a class that is used as a test fixture.

It must be a publicly exported type or NUnit will not see it.

It must have a default constructor or NUnit will not be able to construct it.

It's not clear anyway why you have a constructor in that class that accepts and sets IContext stubIContext as you then go on to mock that field in the Setup.

Remove the public SecurityServiceTests(IContext stubIContext) constructor and the tests will run.

Edit: it's slightly different in NUnit3, as pointed out by @Chris in the comments:

If no arguments are provided with the TestFixtureAttribute, the class must have a default constructor.

If arguments are provided, they must match one of the constructors.

Solution 3

What I had, it's constructor was protected and not public, so Nunit couldn't found it.

Share:
16,840
user1789573
Author by

user1789573

Updated on July 23, 2022

Comments

  • user1789573
    user1789573 almost 2 years

    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);
        }
    
    
    }
    
  • Chris
    Chris about 8 years
    Just to note that in NUnit3, TestFixtures can be parameterized. But you're right, this doesn't look like what's wanted here. v3 Docs