RoLYroLLs
RoLYroLLs

Reputation: 3215

Moq to NSubstitute AddScoped

I'm new to unit testing, so I'm not sure what I'm looking for. I'm trying to convert a project using Moq to NSubstitute.

Question: How do you convert this line from Moq to NSubstitute

services.AddScoped(provider => Mock.Of<ICurrentUserService>(s => s.UserId == _currentUserId));

EDIT 1:

I tried the following with an error message: The delegate type cannot be inferred

services.AddScoped(provider => Substitute.For<ICurrentUserService>(s => s.UserId == _currentUserId));

This line comes from the CleanArchitectureWithBlazorServer project at this line.

EDIT 2:

ICurrentUserService source:

public interface ICurrentUserService {
    string? UserId { get; }
    string? UserName { get; }
    string? TenantId { get; }
    string? TenantName { get; }
}

Upvotes: 1

Views: 77

Answers (1)

Nkosi
Nkosi

Reputation: 247393

A review of the documentation here would have shown you how configure properties on the mock

Based on the interface provided in your example and code it is meant to replace, it would look something like this:

services.AddScoped<ICurrentUserService>(provider => {
    var mock = Substitute.For<ICurrentUserService>()
    mock.UserId.Returns(_currentUserId);
    
    //...set other properties as needed
    
    return mock;
});

If you review the main documentation, it should help you get a better understanding of this .Net mocking library

Upvotes: 1

Related Questions