Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nunit test gives result OneTimeSetUp: No suitable constructor was found

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


}
like image 277
user1789573 Avatar asked Apr 19 '16 15:04

user1789573


People also ask

What is OneTimeSetUp in NUnit?

This attribute is to identify methods that are called once prior to executing any of the tests in a fixture. It may appear on methods of a TestFixture or a SetUpFixture. OneTimeSetUp methods may be either static or instance methods and you may define more than one of them in a fixture.

What is TestFixture NUnit?

The [TestFixture] attribute denotes a class that contains unit tests. The [Test] attribute indicates a method is a test method. Save this file and execute the dotnet test command to build the tests and the class library and run the tests. The NUnit test runner contains the program entry point to run your tests.


2 Answers

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.

like image 82
Charlie Avatar answered Oct 18 '22 11:10

Charlie


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.

like image 40
stuartd Avatar answered Oct 18 '22 12:10

stuartd