An open API service indexing awesome lists of open source software.

https://github.com/hamedstack/hamedstack.nsubstitute

A NSubstitute-based utility library elevates testing and mocking, enhancing their effectiveness and efficiency.
https://github.com/hamedstack/hamedstack.nsubstitute

csharp csharp-library dotnet dotnet-core dotnetcore mock mocking nsubstitute test testing utilities utility utility-library

Last synced: 4 months ago
JSON representation

A NSubstitute-based utility library elevates testing and mocking, enhancing their effectiveness and efficiency.

Awesome Lists containing this project

README

        

### Introduction

Testing protected members within classes can be challenging due to their limited accessibility outside the class's scope. This library provided offers an elegant solution for invoking protected methods, facilitating the testing process. This tutorial will guide you through using these extension methods to test protected members of your classes with NSubstitute, a popular .NET mocking library.

### Using HamedStack.NSubstitute for Protected Members

The library introduces two extension methods:

1. **Expression-based Invocation**: `Protected(this T target, Expression> methodExpression, params object[] args)`
2. **Name-based Invocation**: `Protected(this object target, string methodName, params object[] args)`

These methods facilitate the invocation of protected methods either through an expression or by specifying the method name.

#### 1. Expression-based Invocation

This method allows you to invoke a protected method by specifying it through a lambda expression, offering type safety and refactor resilience.

**Example Usage:**

```csharp
var fooInstance = new Foo();
var result = fooInstance.Protected(f => f.SomeProtectedMethod(0), 5);
Console.WriteLine(result);
```

**Steps:**

1. Instantiate the class (`Foo`) containing the protected method.
2. Use the `Protected` extension method, passing a lambda that mimics calling the protected method. The lambda's arguments are placeholders and will be replaced by the actual parameters you pass next.
3. Provide the actual parameters for the method call.
4. Use the result of the invocation as needed in your tests.

#### 2. Name-based Invocation

This approach allows you to specify the protected method's name as a string, offering flexibility at the expense of type safety.

**Example Usage:**

```csharp
var fooInstance = new Foo();
var result = fooInstance.Protected("SomeProtectedMethod", 5);
Console.WriteLine(result);
```

**Steps:**

1. Create an instance of the class under test.
2. Invoke the `Protected` method on this instance, specifying the protected method's name and its arguments.
3. Process the invocation result as necessary.

### Important Considerations

- The protected method you aim to invoke **must be virtual**. This requirement is due to the need for overriding behavior, which is not feasible with non-virtual methods.
- An `Exception` will be thrown if the specified method does not meet the criteria (e.g., not found or not virtual). It's prudent to wrap your invocation within a try-catch block during testing to gracefully handle such exceptions.