How to iterate through a combination of POCO properties using It.IsIn (someRange) for multiple fields?

I have a POCO class:

public class SomeEntity {
  public int Id { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

I want to test some other classes using different values ​​in the SomeEntity class. The problem is that I need to check various combinations of many properties. For example:

  • Id = 1, FirstName = null, LastName = "Doe"
  • Id = 1, FirstName = "", LastName = "Doe"
  • Id = 1, FirstName = "John", LastName = "Doe"
  • Id = 1, FirstName = null, LastName = ""
  • and etc.

Therefore, in each test, I want to create a test object as follows:

// test that someOtherObject always return the same result 
// for testObject with ID = 1 and accepts any values from range of 
// values for FirstName and LastName

var testObject = new SomeEntity {
  Id = 1, // this must be always 1 for this test
  FirstName = It.IsIn(someListOfPossibleValues), // each of this values must be accepted in test
  LastName = It.IsIn(someListOfPossibleValues) // each of this values must be accepted in test
}

var result = someOtherObject.DoSomething(testObject);

Assert.AreEqual("some expectation", result);

I do not want to use nunit TestCase, as there will be many combinations (huge matrices).

, DoSomething .

: ?

+4
2

It.IsIn ; , Verify , , Setup , Moq. . , NUnit ValuesAttribute ValueSourceAttribute, .

NUnit - , , , ? , NUnit CombinatorialAttribute, NUnit . - :

[Test]
[Combinatorial]
public void YourTest(
    [Values(null, "", "John")] string firstName, 
    [Values(null, "", "Doe")] string lastName)
{
    var testObject = new SomeEntity {
        Id = 1, // this must be always 1 for this test
        FirstName = firstName,
        LastName = lastName
    };

    var result = someOtherObject.DoSomething(testObject);

    Assert.AreEqual("some expectation", result);
}

9 (3 , 3 * 3). , .

, , , , .

+3

FirstName LastName , , 2

foreach (var fname in firstNamePossibleValues) {
    foreach (var lname in lastNamePossibleValues) {
        var testObject = new SomeEntity {
           Id = 1, // this must be always 1 for this test
           FirstName = fname, // each of this values must be accepted in test
           LastName = lname // each of this values must be accepted in test
        }

        var result = someOtherObject.DoSomething(testObject);
        Assert.AreEqual("some expectation", result);
    }
}
0

Source: https://habr.com/ru/post/1623137/


All Articles