diff --git a/matchers/assignable_to_type_of_matcher.go b/matchers/assignable_to_type_of_matcher.go index 89a1fc211..51f8be6ae 100644 --- a/matchers/assignable_to_type_of_matcher.go +++ b/matchers/assignable_to_type_of_matcher.go @@ -12,8 +12,12 @@ type AssignableToTypeOfMatcher struct { } func (matcher *AssignableToTypeOfMatcher) Match(actual interface{}) (success bool, err error) { - if actual == nil || matcher.Expected == nil { + if actual == nil && matcher.Expected == nil { return false, fmt.Errorf("Refusing to compare to .\nBe explicit and use BeNil() instead. This is to avoid mistakes where both sides of an assertion are erroneously uninitialized.") + } else if matcher.Expected == nil { + return false, fmt.Errorf("Refusing to compare type to .\nBe explicit and use BeNil() instead. This is to avoid mistakes where both sides of an assertion are erroneously uninitialized.") + } else if actual == nil { + return false, nil } actualType := reflect.TypeOf(actual) diff --git a/matchers/assignable_to_type_of_matcher_test.go b/matchers/assignable_to_type_of_matcher_test.go index 63c078183..471a46d97 100644 --- a/matchers/assignable_to_type_of_matcher_test.go +++ b/matchers/assignable_to_type_of_matcher_test.go @@ -26,5 +26,21 @@ var _ = Describe("AssignableToTypeOf", func() { Expect(success).Should(BeFalse()) Expect(err).Should(HaveOccurred()) }) + + Context("When actual is nil and expected is not nil", func() { + It("should return false without error", func() { + success, err := (&AssignableToTypeOfMatcher{Expected: 17}).Match(nil) + Expect(success).Should(BeFalse()) + Expect(err).ShouldNot(HaveOccurred()) + }) + }) + + Context("When actual is not nil and expected is nil", func() { + It("should error", func() { + success, err := (&AssignableToTypeOfMatcher{Expected: nil}).Match(17) + Expect(success).Should(BeFalse()) + Expect(err).Should(HaveOccurred()) + }) + }) }) })