Search code examples
c#unit-testingcode-contracts

How unit test on Code Contract should look like?


I have this method in ContractClass:

  public UserModel GetUser(string groupName, string login) {
     Contract.Requires(groupName != null);
     Contract.Requires(groupName != string.Empty);
     Contract.Requires(login != null);
     Contract.Requires(login != string.Empty);

     return default(UserModel);
  }

How would you test this? Do you test all combinations of all possible scenarios when contract fails (ex: groupName is empty and login is null ... and so on)?


Solution

  • Yes, otherwise

    • you have not tested all possible faults,
    • and you have dependencies in your tests.

    E.g.: If you only test that it fails when groupName is equal to null and login is equal to null, and your test fails - you can not be sure that it fails for the correct condition. And what's even more important: You can not even be sure that you have all the correct and important calls to Contract.Requires.