Browse files

Fixed null reference exception where sequence property is null on the…

… 'actual' object.
  • Loading branch information...
1 parent e56366e commit ac45687cb8b2bb8fad3fa891120d2924e0f87281 @mdwhatcott mdwhatcott committed with agross Dec 27, 2012
View
21 Source/Machine.Specifications.Specs/ShouldBeLikeSpecs.cs
@@ -298,6 +298,27 @@ public class and_the_objects_are_different_and_have_null_values
[1]
}");
}
+ public class and_the_objects_are_different_and_the_actual_object_has_a_null_value
+ {
+ Establish context = () => { Obj2 = new Dummy { Prop1 = null }; };
+
+ Because of = () =>
+ {
+ Exception = Catch.Exception(() => Obj2.ShouldBeLike(Obj1));
+ };
+
+ It should_throw_a_specification_exception = () => Exception.ShouldBeOfType<SpecificationException>();
+
+ It should_contain_message = () => Exception.Message.ShouldEqual(@"""Prop1"":
+ Expected: System.Int32[]:
+{
+ [1],
+ [1],
+ [1]
+}
+
+ But was: [null]");
+ }
}
[Subject(typeof(ShouldExtensionMethods))]
View
7 Source/Machine.Specifications/ExtensionMethods.cs
@@ -634,7 +634,12 @@ static IEnumerable<SpecificationException> ShouldBeLikeInternal(object obj, obje
}
else if (nodeType == typeof(ObjectGraphHelper.SequenceNode))
{
- var actualNode = ObjectGraphHelper.GetGraph(obj);
+ if (obj == null)
+ {
+ var errorMessage = PrettyPrintingExtensions.FormatErrorMessage(null, expected);
+ return new[] {NewException(string.Format("{{0}}:{0}{1}", Environment.NewLine, errorMessage), nodeName)};
+ }
+ var actualNode = ObjectGraphHelper.GetGraph(obj);
if (actualNode.GetType() != typeof(ObjectGraphHelper.SequenceNode))
{
var errorMessage = string.Format(" Expected: Array or Sequence{0} But was: {1}", Environment.NewLine, obj.GetType());

0 comments on commit ac45687

Please sign in to comment.