1

I have a test class and I want write a test for one of my functions and I have to use HashSet in [Theory] inlineData but I can't use it.

[Theory]
[InlineData(new HashSet<string>() {"text1","text2"}, "str")]
public void BuildInvertedIndexTest(ISet<string> expectedDocContain, string searchingWord)

I wrote classData and memberData but It wasn't successful. please guide me.

N Th
  • 35
  • 5

2 Answers2

0

You cannot use HashSet<T> with [InlineData] as attributes only support simple types like string, int, bool, arrays etc.

You will need to use another way of parameterising the test, such as [MemberData] for ISet<T>/HashSet<T> which you can read more about in this blog post by Andrew Lock: https://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdata/

Alternatively, you could use a string[] with [InlineData] and construct the HashSet<string> from that in the body of the test.

[Theory]
[InlineData(new string[] {"text1","text2"}, "six")]
public void BuildInvertedIndexTest(string[] expectedDocContain, string searchingWord)
{
    var hashSet = new HashSet<string>(expectedDocContain);
    // TODO Put the rest of your test here.
}
Martin Costello
  • 9,672
  • 5
  • 60
  • 72
0

With MemberDataAttribute the solution could look like this:

public static IEnumerable<object[]> BuildInvertedIndexTestData()
{
    yield return new object[] { new HashSet<string>() { "text1", "text2" }, "str" };
    yield return new object[] { ... };
}

The usage would look like this:

[Theory, MemberData(nameof(BuildInvertedIndexTestData))]
public void BuildInvertedIndexTest(ISet<string> expectedDocContain, string searchingWord)

I haven't tested this solution so it might happen that you need to change the expectedDocContain parameter's type to HashSet<string>.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75