-1

I am writing integration and unit tests and have a static TestData.cs class which provides test data, typically into the Arrange section of each test, for example...

// Arrange
var hol = TestData.Holiday().Where(h => h.Year == 2020 && h.Description == "Star Wars Day").Single();
var user = TestData.TestUser().UserId();

// Act
var newHol = await Repository.AddHoliday(hol, user);

When I come to assert the property values of the new object, should I do a) or b), or something different?

a)

// Assert
Assert.NotNull(newHol);
Assert.IsType<CalHoliday>(newHol);
Assert.Equal(new DateTime(2020, 05, 04), newHol.HolidayDate);
Assert.Equal(2020, newHol.Year);
Assert.Equal("Star Wars Day", newHol.Description);
Assert.Equal("TestUser", newHol.CreateUserId);

b)

// Assert
Assert.NotNull(newHol);
Assert.IsType<CalHoliday>(newHol);
Assert.Equal(hol.HolidayDate, newHol.HolidayDate);
Assert.Equal(hol.Year, newHol.Year);
Assert.Equal(hol.Description, newHol.Description);
Assert.Equal(user.UserId, newHol.CreateUserId);
Drew
  • 477
  • 1
  • 5
  • 16
  • What is your gut feeling and makes most logical sense to you? Explain that feeling and you will likely have your answer – TheGeneral Nov 18 '20 at 02:39
  • I'm really sitting on the fence - the advantages of b) are that it feels cleaner, and if the TestData values change the tests don't need to be updated. But at the same time it doesn't feel as though its a stringent enough test, although I don't understand why I feel that way? If most people said b) is fine, I'd definitely go with b) – Drew Nov 18 '20 at 02:48
  • This site doesn't do well with opinion-based questions. I think your last comment pushed things over the "looking for opinions" line. – Flydog57 Nov 18 '20 at 02:57
  • @Flydog57 No I'm after which is the more correct. My comment was in response to someone much more experienced than me asking me to explore my gut feelings. – Drew Nov 18 '20 at 03:13
  • 1
    For what its worth. Tests should be testing against test data... You know what your test data is, its in some class. In my opinion that makes the most sense. IMO id go B – TheGeneral Nov 18 '20 at 03:15
  • Unless those same string for assertions (test data) are heavily used everywhere, I would keep the test data directly on the asserts. Less code to maintain and faster to debug since you don't have to go check what the value was on the referenced class. – mrbitzilla Nov 18 '20 at 05:13

1 Answers1

0

I would chose “A” because is easier to read.

This is another option that does not hardcode the expected result and is easy to read:

// Arrange
var expectedYear = 2020;
...
// Act
Assert.Equal(expectedYear,newHol.Year);
...

If you use some nuggets like fluent assertions you could do this:

// Assert
newHol.Year.Should.Be(expectedYear); 
...

Which I believe is even better to read.

Julio Cachay
  • 780
  • 5
  • 10