Solved

using Moq with lambda expression in C#

Posted on 2014-09-10
3
300 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 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

20 Experts available now in Live!

Get 1:1 Help Now