-1

I have dot net core (v5) web api porject.

I want to make a Xunit test for testing my web api.

I have a UsersController for CRUD . in my Constructor i have 4 input as shown below:

public UsersController(AppDbContext _appDbContext, IMapper _mapper, IOptions<SieveOptions> sieveOptions, SeiveProcessorService sieveProcessorService) : base(_appDbContext, _mapper)
{
      _sieveProcessorService = sieveProcessorService;
      _sieveOptions = sieveOptions.Value;
}

SeiveProcessorService its a Service in my project.

I can pass three input in my test unit but i can't pass last input.

i dont know how to send to controller

my test code is :

        [Fact]
        public void Test_GetList()
        {
            using (var context = new AppDbContext(ContextOptions))
            {

                //auto mapper configuration
                var mockMapper = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new MappingProfile(null));
                });
                var mapper = mockMapper.CreateMapper();

                //
                SieveOptions sieveOptions = new SieveOptions() { 
                    CaseSensitive = false,
                    DefaultPageSize = 50,
                    MaxPageSize = 500,
                    ThrowExceptions = true,
                };
                IOptions<SieveOptions> options = Options.Create(sieveOptions);

                // Arrange
                var controller = new UsersController(context, mapper, options,????);

                // Act
                var result = controller.GetList(null);

                // Assert
                var viewResult = Assert.IsType<IResult>(result);
                Assert.Null(viewResult);
            }
        }
    }

I don't know how can send fourth input?

mhsankar
  • 423
  • 5
  • 18
  • 2
    Slap an interface on it, change the constructor to accept the interface, and mock the interface? – ProgrammingLlama May 31 '22 at 05:50
  • @DiplomacyNotWar can you explain ? – mhsankar May 31 '22 at 06:11
  • In addition to Maruf's answer, you'll want to take a look at mocking libraries such as Moq, NSubstitute, and FakeItEasy. There are others too, but Moq is the most popular. You should to substitute a fake (mocked) `SeiveProcessorService` which interfaces make it easy to do. By using `ISeiveProcessorService` instead of `SeiveProcessorService`, the mocking library can easily generate its own implementations for `ISeiveProcessorService`'s methods, which allows you to test things without needing all the dependency tree (which would be integration testing anyway). – ProgrammingLlama May 31 '22 at 06:19
  • Does this answer your question? [Trouble understanding Moq](https://stackoverflow.com/questions/34683150/trouble-understanding-moq) – JHBonarius May 31 '22 at 07:14

1 Answers1

1

Create ISeiveProcessorService intrerface and mock it in your unit testing.

public interface ISeiveProcessorService
{
    void A();
}

public class SeiveProcessorService : ISeiveProcessorService
{
    // implement interface methods
    public void A()
    {
    }
}

UPDATE

in order to use Moq you need to install and import namespace. And in your test method:

Mock<ISeiveProcessorService> _mockSeiveProcessorService new Mock<ISeiveProcessorService>();
  
// set up moq  
_mockSeiveProcessorService.Setup(e => e.A()).Returns("Something");
                      
var controller = new UsersController(context, mapper, options,_mockSeiveProcessorService.Object);
Maruf
  • 354
  • 3
  • 8