lundi 25 juillet 2016

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.

RestFailureException.cs

public RestFailureException(IResponseMessage msg, string methodName)

   {

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

    }

My test Methods

Method 1

[TestMethod()]

public void TestFail()

{

        InitMoqVariables();

        int groupId = 0;

        DataModel.Group.Get.Group group = new DataModel.Group.Get.Group();
        string url = string.Format("api/1/groups/{0}", groupId);

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

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

        var client = new AcronisAccountManagementClient(clientFactory.Object, serverUri, moqLogAdapter.Object);
        var result = client.GetGroup(groupId);
        Assert.AreEqual(result, null);

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

Method 2

[TestMethod()]

public void TestBadRequest()

  {

        InitMoqVariables();

        var httpStatusCode = System.Net.HttpStatusCode.BadRequest;

        string groupName = "utGroupName";
        int parentGroupId = 1;
        string url = string.Format("api/1/groups/{0}/children", parentGroupId);
        DataModel.Group.Post.Children group = new DataModel.Group.Post.Children();

        UserResponse userResponse = new UserResponse();

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

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

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

        var result = client.CreateGroup(group, parentGroupId);
        Assert.AreEqual(result.statusCode, httpStatusCode);

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

Aucun commentaire:

Enregistrer un commentaire