My goal is static contract checking to catch / avoid passing around nulls, and I'm new to contracts.
I'm using Contract.Requires(param!=null)
in a method (Link ctor
) and as a test, I call it with null parameters, and I expect it to warn me, but it does not.
The unit test passes, so runtime behavior is as expected.
Why isn't it warning me of the nulls going into the function that can't take nulls?
Class under test:
using System.Diagnostics.Contracts;
public class Link
{
private readonly Shape origin;
private readonly Shape destination;
public Link(Shape origin, Shape destination)
{
Contract.Requires<ArgumentException>(origin != null);
Contract.Requires<ArgumentException>(destination != null);
this.origin = origin;
this.destination = destination;
}
public string OriginID()
{
return origin.ID;
}
public string DestinationID()
{
return destination.ID;
}
}
Unit Test:
using Moq;
using NUnit.Framework;
using Simmons.UI.Controls.Diagram;
namespace Unit
{
/// <summary>
/// A test class for Link
/// </summary>
[TestFixture()]
public class LinkShould
{
private Link link;
[Test()]
[ExpectedException(typeof(ArgumentException))]
public void ExplodeWhenLinkIsCreatedWithNulls()
{
//set up
link = new Link(null, null); // No warning on this line
}
}
And here are my settings in the test project: