Solved

using Moq with lambda expression in C#

Posted on 2014-09-10
3
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TCP Chat/GPS application security thru firewall 4 55
How we can add Dten600.dll in GAC in c#. 18 54
asp.net mvc5 6 33
Example code 13 42
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

732 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