Skip to content

Commit 88998b2

Browse files
committed
Use NullSubstituteReferenceException on null obj
1 parent 48f7996 commit 88998b2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/NSubstitute/Extensions/ProtectedExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Reflection;
22
using NSubstitute.Core;
33
using NSubstitute.Core.Arguments;
4+
using NSubstitute.Exceptions;
45

56
// Disable nullability for client API, so it does not affect clients.
67
#nullable disable annotations
@@ -17,11 +18,11 @@ public static class ProtectedExtensions
1718
/// <param name="methodName">Name of the method.</param>
1819
/// <param name="args">The method arguments.</param>
1920
/// <returns>Result object from the method invocation.</returns>
20-
/// <exception cref="System.ArgumentNullException">Substitute - Cannot mock null object</exception>
21+
/// <exception cref="NSubstitute.Exceptions.NullSubstituteReferenceException">Substitute - Cannot mock null object</exception>
2122
/// <exception cref="System.ArgumentException">Must provide valid protected method name to mock - methodName</exception>
2223
public static object Protected<T>(this T obj, string methodName, params object[] args) where T : class
2324
{
24-
if (obj == null) { throw new ArgumentNullException(nameof(obj), "Cannot mock null object"); }
25+
if (obj == null) { throw new NullSubstituteReferenceException(); }
2526
if (string.IsNullOrWhiteSpace(methodName)) { throw new ArgumentException("Must provide valid protected method name to mock", nameof(methodName)); }
2627

2728
IList<IArgumentSpecification> argTypes = SubstitutionContext.Current.ThreadContext.PeekAllArgumentSpecifications();
@@ -41,11 +42,11 @@ public static object Protected<T>(this T obj, string methodName, params object[]
4142
/// <param name="methodName">Name of the method.</param>
4243
/// <param name="args">The method arguments.</param>
4344
/// <returns>WhenCalled&lt;T&gt;.</returns>
44-
/// <exception cref="System.ArgumentNullException">Substitute - Cannot mock null object</exception>
45+
/// <exception cref="NSubstitute.Exceptions.NullSubstituteReferenceException">Substitute - Cannot mock null object</exception>
4546
/// <exception cref="System.ArgumentException">Must provide valid protected method name to mock - methodName</exception>
4647
public static WhenCalled<T> When<T>(this T obj, string methodName, params object[] args) where T : class
4748
{
48-
if (obj == null) { throw new ArgumentNullException(nameof(obj), "Cannot mock null object"); }
49+
if (obj == null) { throw new NullSubstituteReferenceException(); }
4950
if (string.IsNullOrWhiteSpace(methodName)) { throw new ArgumentException("Must provide valid protected method name to mock", nameof(methodName)); }
5051

5152
IList<IArgumentSpecification> argTypes = SubstitutionContext.Current.ThreadContext.PeekAllArgumentSpecifications();

0 commit comments

Comments
 (0)