Code Contracts not working in Windows Phone 8 unit

2019-08-07 16:51发布

问题:

When my unit test runs a method that has Contract.<>, it is failing with an error:

An exception occurred while invoking executor 'executor://mswptestadapter/v1': Object reference not set to an instance of an object.
========== Run test finished: 0 run (0:00:28.8388006) ==========

Here is contracts settings in pxproj file.

    <CodeContractsEnableRuntimeChecking>False</CodeContractsEnableRuntimeChecking>
    <CodeContractsRuntimeOnlyPublicSurface>False</CodeContractsRuntimeOnlyPublicSurface>
    <CodeContractsRuntimeThrowOnFailure>True</CodeContractsRuntimeThrowOnFailure>
    <CodeContractsRuntimeCallSiteRequires>False</CodeContractsRuntimeCallSiteRequires>
    <CodeContractsRuntimeSkipQuantifiers>False</CodeContractsRuntimeSkipQuantifiers>
    <CodeContractsRunCodeAnalysis>False</CodeContractsRunCodeAnalysis>
    <CodeContractsNonNullObligations>True</CodeContractsNonNullObligations>
    <CodeContractsBoundsObligations>True</CodeContractsBoundsObligations>
    <CodeContractsArithmeticObligations>False</CodeContractsArithmeticObligations>
    <CodeContractsEnumObligations>False</CodeContractsEnumObligations>
    <CodeContractsRedundantAssumptions>False</CodeContractsRedundantAssumptions>
    <CodeContractsMissingPublicRequiresAsWarnings>True</CodeContractsMissingPublicRequiresAsWarnings>
    <CodeContractsInferRequires>True</CodeContractsInferRequires>
    <CodeContractsInferEnsures>False</CodeContractsInferEnsures>
    <CodeContractsInferObjectInvariants>False</CodeContractsInferObjectInvariants>
    <CodeContractsSuggestAssumptions>False</CodeContractsSuggestAssumptions>
    <CodeContractsSuggestRequires>True</CodeContractsSuggestRequires>
    <CodeContractsSuggestObjectInvariants>False</CodeContractsSuggestObjectInvariants>
    <CodeContractsRunInBackground>True</CodeContractsRunInBackground>
    <CodeContractsShowSquigglies>True</CodeContractsShowSquigglies>
    <CodeContractsUseBaseLine>False</CodeContractsUseBaseLine>
    <CodeContractsEmitXMLDocs>False</CodeContractsEmitXMLDocs>
    <CodeContractsCustomRewriterAssembly />
    <CodeContractsCustomRewriterClass />
    <CodeContractsLibPaths />
    <CodeContractsExtraRewriteOptions />
    <CodeContractsExtraAnalysisOptions />
    <CodeContractsSQLServerOption />
    <CodeContractsBaseLineFile />
    <CodeContractsCacheAnalysisResults>True</CodeContractsCacheAnalysisResults>
    <CodeContractsFailBuildOnWarnings>False</CodeContractsFailBuildOnWarnings>
    <CodeContractsRuntimeCheckingLevel>Full</CodeContractsRuntimeCheckingLevel>
    <CodeContractsReferenceAssembly>%28none%29</CodeContractsReferenceAssembly>
    <CodeContractsAnalysisWarningLevel>0</CodeContractsAnalysisWarningLevel>

回答1:

I was missing Code Contracts for .Net and Visual Studio 2012 extension:

Code Contracts for .Net - http://www.google.com/url?q=http%3A%2F%2Fvisualstudiogallery.msdn.microsoft.com%2F1ec7db13-3363-46c9-851f-1ce455f66970%3FSRC%3DVSIDE&sa=D&sntz=1&usg=AFQjCNGLZoTI_GSQJfSfJP5JuSNvv8SlOw

Code Contracts Editor Extensions VS2012 - http://www.google.com/url?q=http%3A%2F%2Fvisualstudiogallery.msdn.microsoft.com%2F02de7066-b6ca-42b3-8b3c-2562c7fa024f%3FSRC%3DVSIDE&sa=D&sntz=1&usg=AFQjCNEHZ2ObOxX0KF5PK7DAme9C1zh6Kg