Jump to content

Help needed in Moq Unit testing for Web api


Recommended Posts

Posted

Hi,

I need help in writing Moq unit test for the below criteria.

For instance, i have a Person class/model. I need to save this object in database and return the same model back to the client as in HttpResponse type with statuscode success and content to be the object.

I need to write moq unit test for this case. I am stuck in setting the HttpResponse Moq object for content. I am able to set the status.

Could you please advise.

Posted
3 minutes ago, MegaPowerRockstar said:

No one has idea ??

 

3 minutes ago, MegaPowerRockstar said:

No one has idea ??

u have to mock the saving call that's it . Pm me the code 

 

Var moqobj = ne moq<service>

Moqobj.setup(x=>x.savingmethod()).......

Posted
53 minutes ago, afdbzindabad said:

 

u have to mock the saving call that's it . Pm me the code 

 

Var moqobj = ne moq<service>

Moqobj.setup(x=>x.savingmethod()).......

 

This is the below code i am trying to set the moq object

var mockResponseMessage = new Mock<HttpResponseMessage>();
mockResponseMessage.Object.StatusCode = HttpStatusCode.OK;
mockResponseMessage.Object.Content = ??

 

The below code is the one that i am using in the implementation

Person objPerson = _response.Content.ReadAsAsync<Person>().Result;

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...