Skip to content

Commit

Permalink
Use NullSubstituteReferenceException on null obj
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason31569 committed Dec 20, 2024
1 parent 48f7996 commit 88998b2
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/NSubstitute/Extensions/ProtectedExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using NSubstitute.Core;
using NSubstitute.Core.Arguments;
using NSubstitute.Exceptions;

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

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

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

0 comments on commit 88998b2

Please sign in to comment.