Solved

using Moq with lambda expression in C#

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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