To write a unit test in xUnit to validate MAC addresses in C#, you can use the following code:
main.cs923 chars36 lines
In this code, we have a unit test class named MacAddressValidatorTest
. Inside this class, we have a single theory named IsMacAddress_ValidatesMacAddress
which takes two parameters: a MAC address and the expected result of the validation.
The [InlineData]
attribute is used to pass test data to the IsMacAddress_ValidatesMacAddress
theory. We pass the MAC address and its expected validation result for each test case.
The MacAddressValidator
class contains a single static method named IsMacAddress
which takes a MAC address as a string and returns a boolean indicating whether the address is valid or not.
The PhysicalAddress.Parse
method is used to parse the MAC address string and throw an exception if the address is not valid. If an exception is thrown, the IsMacAddress
method returns false. Otherwise, it returns true.
Finally, the Assert.Equal
method is used to compare the actual result of the validation with the expected result, and fail the test if they are not the same.
gistlibby LogSnag