1

I'm trying to unit test a controller and I created a fake class that Implements an Interface.

public class UnitTest1   
    {
        GrantProgramsController _controller;
        IBLGrantProgram _grant;
        readonly ILogger<GrantProgramsController> _logger;
        public UnitTest1()
        {
             _grant = new BLGrantProgramFake();
            _logger = new Logger<GrantProgramsController>(new NullLoggerFactory());
            _controller = new GrantProgramsController(_grant, _logger);
          
        }
        //tests for get method
        [Fact]
        public void Get_whencalled_returnsokresult()
        {
            // Act
            var okResult = _controller.GetGrantProgram();
            // Assert
            Assert.IsType<OkObjectResult>(okResult.Result);
        }
----------

But, I'm trying to use Moq framework and mock the interface instead of creating a fake implementation for the interface.

 public UnitTest1()
        {
            // _grant = new BLGrantProgramFake();
            _grant = new Mock<BLGrantProgram>();
            _logger = new Logger<GrantProgramsController>(new NullLoggerFactory());
            _controller = new GrantProgramsController(_grant, _logger);
          
        }

but error pops up for mocking. can somebody point out whether if this is not the way. I'm new to this. Thanks in advance.

1
  • You are trying to mock a class not it's interface (unless yor interface name lacks the "I" prefix), which depending on what you are trying to do with you mock, could throw an error. In additoin, sharing the exact error oyou're encountering could allow us to exactly know what is going on Commented Apr 1, 2021 at 13:09

1 Answer 1

0

This may just be an assignment issue since when using MOQ the assignment to the actual type will need to come from the Mock<T>.Object property

public UnitTest1() {
    _grant = new Mock<IBLGrantProgram>().Object; //<---
    _logger = new Logger<GrantProgramsController>(new NullLoggerFactory());
    _controller = new GrantProgramsController(_grant, _logger);
}

If there are members to be configured then the Mock<T> would still be needed to setup the expectations.

For example

public UnitTest1() {
   _grant = new Mock<IBLGrantProgram>().Object; //<--- mock assigned

    //extract Mock<T> and setup expectations.
    Mock.Get(_grant).Setup(_ => _.SomeMember()).Returns(someValue);

    _logger = new Logger<GrantProgramsController>(new NullLoggerFactory());
    _controller = new GrantProgramsController(_grant, _logger);
}

I would suggest you Reference Moq Quickstart to get a better understanding of how to use that library.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.