Question

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)?

Was it helpful?

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.

OTHER TIPS

I wouldn't write any unit tests for those Code Contracts at all. If your preconditions are wrong, then peer code review is a better way of picking that up. If your preconditions are correct, then all you'll end up testing is that the Code Contract engine works, not that your code works.

It might be worth running integration tests to check that your preconditions are not too strict, but in most cases the output of the static verifier will be a better guide.

If a precondition becomes particularly complex, then there's an argument for unit testing it. However, in that instance, I'd be tempted to extract the precondition into a [Pure] method, and unit test that instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top