Mock and Dependency Redundancy
- Dominant language
- C#
- Stars
- 4.5k
- Forks
- 836
- Avg merge
- 6d 3h
- Merged PRs (30d)
- 7
Description
### Is your feature request related to a problem? Please describe.
We recently stumbled over the issue of testing specific scenarios with the gRPC Channel object.
The issue was simplified (without the project overhead).
#### Example issue:
```csharp
public class CustomGrpcService
{
public CustomGrpcService()
{
this.GrpcChannel = GrpcChannel.ForAddress("testUri");
}
public GrpcChannel GrpcChannel { get; set; }
public void DoSomething()
{
if (this.GrpcChannel.State == ConnectivityState.Idle)
{
// Do Smth
}
else if (this.GrpcChannel.State == ConnectivityState.Ready)
{
// Do Smth
}
else if (this.GrpcChannel.State == ConnectivityState.Connecting)
{
// Do Smth
}
else if (this.GrpcChannel.State == ConnectivityState.TransientFailure)
{
// Do Smth
}
else if (this.GrpcChannel.State == ConnectivityState.Shutdown)
{
// Do Smth
}
}
}
```
#### Example tests:
```csharp
[Fact]
public void TestIdleBehaiour()
{
CustomGrpcService customGrpcService = new CustomGrpcService();
customGrpcService.DoSomething();
// Assert result in some way
}
[Fact]
public void TestReadyBehaiour()
{
CustomGrpcService customGrpcService = new CustomGrpcService();
customGrpcService.DoSomething();
// Assert result in some way
}
....
```
As of now, we have no possibility to change the behavior of the channel. Also, we would like to test this kind of functionality without acquiring a real OS port.
Mocking is unfortunately not an option, due to the class `GrpcChannel` being sealed.
### Describe the solution you'd like
I would like a possibility to abstract the GrpcChannel object in some way.
#### Define interface to decouple functionallity from implementation
```csharp
public interface IGrpcChannel: IDisposable
{
public ConnectivityState State { get; }
public Task ConnectAsync(CancellationToken cancellationToken = default);
....
}
```
#### Open up sealed class
Remove the sealed keyword from the 'GrpcChannel' to allow a mock implementation.
### Additional context
I believe that some applications could really benefit from this functionality. If it fits into the current project architecture, I would be glad to contribute.
Best
Fabian
Contributor guide
Research direction
Start by examining the GrpcChannel API and its sealed-class constraint, then compare the proposed IGrpcChannel abstraction with allowing subclassing. Done means selecting and documenting an approach that permits controlled channel-state testing without acquiring a real OS port, while preserving the existing channel functionality.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100