Solved

using Moq with lambda expression in C#

Posted on 2014-09-10
3
307 Views
Last Modified: 2014-09-20
Hi Experts,

I am writing unit test using moq and have following code

 public interface IsampleData
    {
     
       UserResponse GetDetails();
      
    }

public class UserResponse
{

public IEnumerable<usertab> userlist{get;set;}
public string Name {get;set;}
public string state {get;set;}
public string country {get;set}

}

public class usertab
{
public string tname {get;set;}

public string fname {get;set;}

}

Unit test: How can I return userresponse object in the below lambda expression?

 Mock<IsampleData> userfakeObj = new Mock<IsampleData>();

                userfakeObj.Setup(x => x.GetDetails()).Returns  ?

Open in new window

0
Comment
Question by:ksd123
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40315995
Return a new instance (filled in whatever way makes sense for your test). You might consider using NBuilder also.

e.g.

userfakeObj.Setup(x => x.GetDetails())
           .Returns(new UserResponse()
                    {
                        userlist = Enumerable.Empty<usertab>(),
                        Name = "some name",
                        state = "NY",
                        country = "USA",
                    });

Open in new window

0
 

Author Comment

by:ksd123
ID: 40317686
Thank you somuch.It worked.But there is a change in the method signature of the interface ie. with <Task>

1)
 public interface IsampleData
    {
     
      <Task> UserResponse GetDetails();
      
    }

Open in new window




And I have changed lambda expression to the below code.Basically I am writing unit tests with moq.Below I have get property and now I am getting "not all code path returns a value in the lambda expression of type "System.Threading.Tasks.Task" "error. Can you correct me

get

{

userfakeObj.Setup(x => x.GetDetails()).Returns(()=>

{

Task.Run(() =>
        
 {
           return new UserResponse()
                    {
                        userlist = Enumerable.Empty<usertab>(),
                        Name = "some name",
                        state = "NY",
                        country = "USA",
                    };

});

});

return userfakeobj.Object

}

Open in new window


2)I am try  to fake  IEnumerable usertab with some data for the above userresponse class.Please correct me if below code is not the correct way of assigning IEnumerable data

var userdata = new usertab[]{

                   new usertab{tName="Employee",fName="abc"},
                   new usertab{tName="Employee",fName="xyz"},
                  
                };

Open in new window


Thanks in Advance
0
 

Author Closing Comment

by:ksd123
ID: 40334984
Thank you
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now