-1

I am writing test case and getting compile error. Returns is not working in this below statement.

    //Moq
    //Error: '.' cannot be applied to operand of type 'void'. This gives error before ".Returns".
    private Mock<ITestDomain> _testDomain;
   //test
    [TestMethod]
    public void PassParams()
    {
    //arrange
    var opentickResponse = new OpenTicketResponse();
    opentickResponse.Tickets.Add(new Ticket { TickId = "ticket123"});
    opentickResponse.Tickets.Add(new Ticket { TickId = "ticket456"});
    
    
    _testDomain.Setup(x => x.OpenTicketing(null)).Returns(openTickResponse);
    //act
    var response = _target.Execute(_request);
    
    //assert
    Assert.IsNotNull(response, "response = null");
    Assert.AreEqual("name", response.Ticket.Countryname);
    
    
    _testDomain.Verify(x => x.OpenTicket(null)).Returns(x => (x[0] as OpenTicketRequest).phone.ID == _request.phone.ID && 
        (x[0] as OpenTicketRequest).ActiveContract.IDnumber == _request.ActiveContract.IDnumber);
    }

I wrote like this but this shows error. Is the Verify statement wrong?

Alex Dave
  • 108
  • 12
  • 2
    "but this shows error" - which error? – Klaus Gütter Dec 28 '22 at 07:49
  • @KlausGütter Operator '.' cannot be applied to operand of type 'void'. OpenDocument(null)).Returns error shows at this dot. – Alex Dave Dec 28 '22 at 16:38
  • First of all, I am assuming you are writing this in some good IDEs that should have evidently shown you the reason. Second, you must be exploring the documentations for this Mock before using/applying. Refer to [this](https://stackoverflow.com/questions/9136674/verify-a-method-call-using-moq). If this makes sense to you, and helps, am i winning the bounty ;-) ? – Ak777 Jan 04 '23 at 01:49
  • @Ak777 am not understanding what I missed here. I think the link which you sent I have already seen. – Alex Dave Jan 04 '23 at 04:14
  • I mean is this as simple as a typo? Did you forget the `_` before `testDomain.Setup`? Shouldn't it be `_testDomain.Setup`? Also, did you instanciate the Mock? `_testDomain = new Mock();`? – BG_Cw Jan 04 '23 at 10:24
  • What is _target and _request? Can you please post more code? – Viktor Jan 06 '23 at 13:48
  • @Viktor _request is passing arguments. mock sample values. _target is passing interfaces. – Alex Dave Jan 11 '23 at 12:36

4 Answers4

2
  • I'm going to assume this is unit testing.

I see a lot of problems in that code:

  1. You are setting up testDomain without the underscore _ but you are asserting _testDomain with the underscore. Is there a difference between the two? Why are there 2 of these?
  2. You are never mocking the OpenDocument method so inside that target it will return null.
  3. You are mocking a method called OpenDocumentation, but you are asserting that OpenDocument.
  4. I assume your target doesn't just call methods with null parameter in there. So instead of mocking them with null maybe you want to use something like It.IsAny<WhatEverOpenDocumentsReceives>()
  5. In unit testing you are focusing on only one unit to test. In your case, your unit is an instance of the target. EVERYTHING else should be mocked. If everything else is mocked at some point that means that _testDomain / testDomain is not real. Your last statement tries to test if a method from testDomain was called and returned some values. I don't think you need to test that a mocked method returned a certain result. Why would you since you are the one mocking it ?
1

The Moq Method Verify does not return, it throws if the functions it is trying to verify wasn't called on the mock object. It is a void method. So you can't call a.Verify().Returns because a.Verify() is void. That's the cause of the compile error.

As for what you were actually trying to achieve I'm a bit puzzled, you are testing target.Execute, but your code doesn't show you injecting _testDomain into target so it's basically not used as far as I can tell. Maybe you should check whether Moq really does what you think it does. Here is a fair starting point.

BG_Cw
  • 197
  • 8
1

Verify is used to check that a particular method has been called and can be configured to check to amount of times a method was called and the parameters used to call it e.g. the below would check that OpenDocument() is called once with null as the parameter value:

_testDomain.Verify(x => x.OpenDocument(null), Times.Once);

Verify is not used to check results of method calls. If you want to check the result of a method, you need a test that calls the method, records the result and makes assertions on the result. It can even be done in the same test by doing something like this instead of using Verify:

var result = _testDomain.OpenDocument(null);
var openDocumentRequest = result[0] as OpenDocumentRequest;
Assert.AreEqual(_request.phone.ID, openDocumentRequest.phone.ID);
Assert.AreEqual(_request.ActiveContract.IDnumber, openDocumentRequest.ActiveContract.IDnumber);
YungDeiza
  • 3,128
  • 1
  • 7
  • 32
1

To validate the arguments you can use Callback() functionality. In your example it could look like:

    OpenDocumentRequest actualRequest = null;
    _testDomain.Setup(x => x.OpenDocument(It.IsAny<OpenDocumentRequest>()))
        .Callback((OpenDocumentRequest value) => { actualRequest = value; });

    // act
    var response = _target.Execute(_request);

    // validate actual request
    Assert.IsNotNull(actualRequest);
    Assert.AreEqual(_request.phone.ID, actualRequest.phone.ID);
    Assert.AreEqual(_request.ActiveContract.IDnumber, actualRequest.ActiveContract.IDnumber);
Mike Mozhaev
  • 2,367
  • 14
  • 13