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
Solved

using Moq with lambda expression in C#

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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