Given the following example code, how can I configure Pex to respect my Code Contracts?
public static IEnumerable<User> Administrators(this UserGroup userGroup)
{
Contract.Requires(userGroup != null);
Contract.Requires(userGroup.UserList != null);
return userGroup.UserList.Where(ul => ul.IsAdmin == true);
}
Current Problem: When I run Pex, it’s still generating test cases which violate the specified code contracts.
FYI: Here are the ‘Code Contracts’ settings in my csproj file.
EDIT: Did something break in SP1?
First you need to use a typed version of Requires
use ArgumentNullException as T
Also in you project properties you need to tell code cotracts to use the standard rewriter. DO NOT click assert on failure 😉
then your code will throw an argumetn null exception and pex can add an attribute to your pexmethod to say that it is allowed to throw it and will create a passing test that throws the exception
you can then promote those and save the unit tests