Reputation: 535
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);
}
}
Upvotes: 11
Views: 27705
Reputation: 1
The error message was generated due to having a parameterised constructor. I created another constructor without parameter and that resolved the issue.
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class HooksClass : PageTest
{
public IPage _page;
public HooksClass()
{
}
public HooksClass(IPage page)
{
_page = page;
}
Upvotes: 0
Reputation: 9
For me, I was using an in-memory database, and I had a call to "context.Database.Migrate()" which was the cause, I had to comment the call.
var scope = _factory.Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.EnsureDeleted();
//context.Database.Migrate(); // the issue
ApplicationDbContextExtensions.Seed(context);
Upvotes: 0
Reputation: 73313
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.
Upvotes: 6
Reputation: 5529
What I had, it's constructor was protected and not public, so Nunit couldn't found it.
Upvotes: 4
Reputation: 13736
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:
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.
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.
Upvotes: 15