Skip to content

Instantly share code, notes, and snippets.

@kolesnick
Created February 29, 2012 17:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kolesnick/1942519 to your computer and use it in GitHub Desktop.
Save kolesnick/1942519 to your computer and use it in GitHub Desktop.
ReSharper template for MSpec tests of Equals method and == operator
/*
Please remove this comment only after placing this template to ReSharper template editor.
Existance of this comment during paste will automatically align macroses in shown order.
Macroses:
$Entity$ - EntityName
$entity$ - entityName
$entity_in_when_part$ - entity_name
$entities_in_when_part$ - entity_name_in_plural
$usage_of_initial_parameters_in_ctor$ - arguments of factory method supplied to constructor
$initial_parameters_with_default_values$ - declaration of factory method arguments with default values for each argument
$initial_data$ - initial data supplied to factory method
$different_initial_data$ - different initial data supplied to factory method
$object_of_other_type_probably_string$ - instance of different type, e.g. string
*/
public class $Entity$TestsContext
{
protected static $Entity$ Create$Entity$($initial_parameters_with_default_values$)
{
return new $Entity$($usage_of_initial_parameters_in_ctor$);
}
}
#region GetHashCode tests
[Subject(typeof($Entity$))]
public class GetHashCode_when_called_two_times_for_two_identical_$entities_in_when_part$ : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$(
$initial_data$);
identical$Entity$ = Create$Entity$(
$initial_data$);
};
Because of = () =>
{
firstResult = $entity$.GetHashCode();
secondResult = identical$Entity$.GetHashCode();
};
It should_same_results_for_both_calls = () =>
firstResult.ShouldEqual(secondResult);
private static int firstResult;
private static int secondResult;
private static $Entity$ $entity$;
private static $Entity$ identical$Entity$;
}
#endregion // GetHashCode tests
#region Equals tests
[Subject(typeof($Entity$))]
public class Equals_when_called_with_null : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$();
};
Because of = () =>
result = $entity$.Equals(null);
It should_return_false = () =>
result.ShouldBeFalse();
private static bool result;
private static $Entity$ $entity$;
}
[Subject(typeof($Entity$))]
public class Equals_when_called_with_object_of_another_type : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$();
};
Because of = () =>
result = $entity$.Equals($object_of_other_type_probably_string$);
It should_return_false = () =>
result.ShouldBeFalse();
private static bool result;
private static $Entity$ $entity$;
}
[Subject(typeof($Entity$))]
public class Equals_when_called_with_identical_$entity_in_when_part$ : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$(
$initial_data$);
identical$Entity$ = Create$Entity$(
$initial_data$);
};
Because of = () =>
result = $entity$.Equals(identical$Entity$);
It should_return_true = () =>
result.ShouldBeTrue();
private static bool result;
private static $Entity$ $entity$;
private static $Entity$ identical$Entity$;
}
[Subject(typeof($Entity$))]
public class Equals_when_called_with_different_$entity_in_when_part$ : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$(
$initial_data$);
different$Entity$ = Create$Entity$(
$different_initial_data$);
};
Because of = () =>
result = $entity$.Equals(different$Entity$);
It should_return_false = () =>
result.ShouldBeFalse();
private static bool result;
private static $Entity$ $entity$;
private static $Entity$ different$Entity$;
}
#endregion // Equals tests
#region == operator tests
[Subject(typeof($Entity$))]
public class EqualityOperator_when_called_with_two_nulls : $Entity$TestsContext
{
Because of = () =>
result = null as $Entity$ == null as $Entity$;
It should_return_true = () =>
result.ShouldBeTrue();
private static bool result;
}
[Subject(typeof($Entity$))]
public class EqualityOperator_when_called_with_null_and_not_null : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$();
};
Because of = () =>
result = null == $entity$;
It should_return_false = () =>
result.ShouldBeFalse();
private static bool result;
private static $Entity$ $entity$;
}
[Subject(typeof($Entity$))]
public class EqualityOperator_when_called_with_not_null_and_null : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$();
};
Because of = () =>
result = $entity$ == null;
It should_return_false = () =>
result.ShouldBeFalse();
private static bool result;
private static $Entity$ $entity$;
}
[Subject(typeof($Entity$))]
public class EqualityOperator_when_called_with_same_$entity_in_when_part$ : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$();
};
#pragma warning disable 1718
Because of = () =>
result = $entity$ == $entity$;
#pragma warning restore 1718
It should_return_true = () =>
result.ShouldBeTrue();
private static bool result;
private static $Entity$ $entity$;
}
[Subject(typeof($Entity$))]
public class EqualityOperator_when_called_with_two_identical_$entities_in_when_part$ : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$(
$initial_data$);
identical$Entity$ = Create$Entity$(
$initial_data$);
};
Because of = () =>
result = $entity$ == identical$Entity$;
It should_return_true = () =>
result.ShouldBeTrue();
private static bool result;
private static $Entity$ $entity$;
private static $Entity$ identical$Entity$;
}
[Subject(typeof($Entity$))]
public class EqualityOperator_when_called_with_two_different_$entities_in_when_part$ : $Entity$TestsContext
{
Establish context = () =>
{
$entity$ = Create$Entity$(
$initial_data$);
different$Entity$ = Create$Entity$(
$different_initial_data$);
};
Because of = () =>
result = $entity$ == different$Entity$;
It should_return_false = () =>
result.ShouldBeFalse();
private static bool result;
private static $Entity$ $entity$;
private static $Entity$ different$Entity$;
}
#endregion // == operator tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment