Xunit The following constructor parameters did not have matching fixture data
Asked Answered
S

9

22

I keep getting this error while using XUnit for .NET 1.0 framework net46.

The following constructor parameters did not have matching fixture data

I have seen this post: Collection fixture won't inject and followed the instructions regarding collection fixture closely as described here:

http://xunit.github.io/docs/shared-context.html#collection-fixture

Nothing seems to work.

Any suggestions to what might cause this?

Shipboard answered 11/9, 2016 at 21:45 Comment(0)
S
23

In my case it turned out to be a matter of doing it right according to the instructions. By mistake I had annotated the class with

 [Collection("ProjectCollection")]

instead of:

 [Collection("ActorProjectCollection")]

I must say that the dependency injection mechanism of XUnit would be greatly improved if the error messages gave more explicit hint of what is wrong.

Shipboard answered 12/9, 2016 at 7:37 Comment(1)
May help guard against these spelling mistakes by using nameof(ActorProjectCollection)Titos
C
15

Another scenario in which this might fail is if the [CollectionDefinition] is defined on a type outside the executing test assembly. The attribute itself needs to be defined inside of it or xUnit won't pick it up.

Castellanos answered 5/4, 2019 at 19:14 Comment(1)
This happened to me. The class defined by CollectionDefinition and Collection have different namespaces.Coccidioidomycosis
V
6

This exception may arise when the constructor of your fixture class is failing due to some other problem, in my case connecting to a local Mongo server.

Either look for other failures and solve those first or lighten your constructor up so it does less.

Vote answered 8/4, 2019 at 14:58 Comment(0)
D
5

In my case I had to implement the IClassFixture<> interface on the test class.

public class MyTests : IClassFixture<QueryTestFixture>

After I moved the QueryTestFixture to another project the Class attribute stopped working

[Collection("QueryCollection")]

Which was linked to the implementation in the fixture class (see below)

[CollectionDefinition("QueryCollection")]
public class QueryCollection : ICollectionFixture<QueryTestFixture> { }

I had tried many alternative ways to solve this but ended up reading the xunit.net explanation about shared context. which provided a solution.

Dysphonia answered 16/10, 2019 at 19:28 Comment(0)
S
4

It happened to me a couple of times just after adding the Collection and the CollectionDefinition decorators and I always arrive to this answer when looking on Internet.

But in my case the problem was just that it seems that a 'Clean Solution' action is needed before testing whether it works or not. Without cleaning the solution I always get a The following constructor parameters did not have matching fixture data error.

So, I write also this answer in order to help my future self.


Anyway, in order to avoid the problem explained by Nikola Schou, you can always use a constant to avoid name mistmatching:

public static class Collections
{
    public const string ActorProjectCollection= "ActorProjectCollection";
}

-

[Collection(Collections.ActorProjectCollection)]
/// ...

-

[CollectionDefinition(Collections.ActorProjectCollection)]
/// ...
Spirogyra answered 2/3, 2019 at 9:29 Comment(2)
I had the same issue. I couldn't execute multiple tests at once after refactoring my fixtures (they worked if i ran them one by one). After a clean up in VS it worked fine.Schlock
Keep in mind that there should only be one class decorated with CollectionDefinition per collection key.Marismarisa
M
1

definition in the same namespace

  [CollectionDefinition("Integration Tests", DisableParallelization = true)]
    public class TestCollection : ICollectionFixture<WebApplicationFactory<Startup>>
    {
        // This class has no code, and is never created. Its purpose is simply
        // to be the place to apply [CollectionDefinition] and all the
        // ICollectionFixture<> interfaces.
    }

usage in the same namespace

    [Collection("Integration Tests")]
    public class BasicControllerTests
    {
        private readonly WebApplicationFactory<Startup> _factory;

        public BasicControllerTests(WebApplicationFactory<Startup> factory)
        {
            _factory = factory;
        }

 ...

source

Mcspadden answered 15/7, 2020 at 10:56 Comment(0)
R
1

My two cents: looking at the posted responses I checked the following:

  • CollectionDefinition(X) and Collection(X) classes are in the same assembly
  • Both the definition and the classes using it refer to the same collection name
  • The classes using the definition have a constructor receiving the fixture

But still not working... What was the problem? The class with the definition has not the public modifier (class Definition ...) while the classes using the definition have it.

Just added the public to the definition, and it worked like a charm.

Reticulum answered 6/8, 2020 at 15:56 Comment(0)
G
1

in my case i wasnt make FixtureClass public

   [CollectionDefinition(nameof(ApiTestCollection))]
    **public** class ApiTestCollection : ICollectionFixture<OpenSchoolApiBroker>
    {}

Genni answered 29/7, 2021 at 18:16 Comment(0)
T
0

In my case, I had my Fixture inside of the CollectionDefinition instead of the FixtureCollection.

For example:

Fixture Code

public class MyFixture { ... }

Not Working

[CollectionDefinition(nameof(MyFixture))]
public class MyFixtureCollection : ICollection<MyFixture> {}

Working

[CollectionDefinition(nameof(MyFixtureCollection))]
public class MyFixtureCollection : ICollection<MyFixture> {}
Titos answered 1/2, 2022 at 3:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.