Xunit The following constructor parameters did not have matching fixture data

本秂侑毒 提交于 2019-12-08 17:39:56

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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.




回答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.



来源:https://stackoverflow.com/questions/39440942/xunit-the-following-constructor-parameters-did-not-have-matching-fixture-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!