Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq.Mock Exception with invocation failed with mock behavior strict

I am new to Moq framework and I have writtern a test method but I am getting the below error. I couldn't find where I have missed.

Can some one please let me know how can i correct the below error?


An exception of type 'Moq.MockException' occurred in Moq.dll but was not handled in user code

Additional information: IResponseMessage.ReadContentAsString() invocation failed with mock behavior Strict.

All invocations on the mock must have a corresponding setup.

Execp.cs

public Execp(IResponseMessage msg)  
{

    this.StatusCode = msg.StatusCode;//*getting exception here while running **method 1***
    this.ReadContentAsString = msg.ReadContentAsString();//*getting exception here while running **method 2***


}

My test Methods

Method 1

[TestMethod()]        
public void TestFail()
{

    int employeeId = 0;

    DataModel.Employee.Get.Employee employee= new DataModel.Employee.Get.Employee();
    string url = string.Format("api/1/somename/{0}", employeeId);

    restClient
        .Setup(x => x.Get(url))
        .Returns(responseMessage.Object);

    responseMessage.SetupGet(x => x.IsSuccessStatusCode).Returns(false);

    var client = new account(clientFactory.Object, serverUri, moqLogged.Object);
    var result = client.GetEmployee(employeeId);
    Assert.AreEqual(result, null);

    client.Dispose();
    moqFactory.VerifyAll();
}

Method 2

[TestMethod()]
public void TestBadRequest()
{

   var httpStatusCode = System.Net.HttpStatusCode.BadRequest;

    string employeeName = "Test Name";
    int teamLeaderId= 1;
    string url = string.Format("api/1/somename/{0}/teammember", teamLeaderId);
    DataModel.Group.Post.TeamMember employee= new DataModel.Group.Post.teamMember();

    UserResponse userResponse = new UserResponse();

    restClient
        .Setup(x => x.PostAsJson(url, It.IsAny<DataModel.Employee.Post.TeamMember>()))
        .Returns(responseMessage.Object);

    responseMessage.SetupGet(x => x.IsSuccessStatusCode).Returns(false);
    responseMessage.SetupGet(x => x.StatusCode).Returns(httpStatusCode);

    var client = new AcronisAccountManagementClient(clientFactory.Object, serverUri, moqLogged.Object);

    var result = client.CreateEmployee(employee, teamLeaderId);
    Assert.AreEqual(result.statusCode, httpStatusCode);

    client.Dispose();
    moqFactory.VerifyAll();
}
like image 787
crony Avatar asked Jul 26 '16 00:07

crony


1 Answers

You created a Mock<IResponseMessage>, which uses MockBehavior.Strict by default which

MockBehavior.Strict : Causes the mock to always throw an exception for invocations that don't have a corresponding setup.

Somewhere in your code you are invoking members that have no setup configured. I suggest create a setup for all members you intend to invoke during your tests

For method 1 and 2:

//...other code removed for brevity

var httpStatusCode = System.Net.HttpStatusCode.BadRequest;//or what ever you want it to be
responseMessage.Setup(m => m.StatusCode).Returns(httpStatusCode);
responseMessage.Setup(m => m.ReadContentAsString()).Returns("Put your return string here");

//...other code removed for brevity
like image 138
Nkosi Avatar answered Oct 23 '22 00:10

Nkosi