N
N
Nodir Azam2014-11-01 20:30:21
.NET
Nodir Azam, 2014-11-01 20:30:21

Moq Library: How to return a List in an emulated method?

Hello!
I'm trying to use the Moq library for unit testing.
Here is an option that works:

Application app = InitTestApplication(id);
var repositoryMock = new Mock<IRepository> ();
repositoryMock.Setup (x => x.Create (app)).Returns (app);
var repositoryMongo = repositoryMock.Object;
var repository = new ApplicationRepository (repositoryMongo);
var appResult = repository.Create (app);
Assert.AreEqual (app.Id, appResult.Id);

When I try to return a list as a result, I get null.
repositoryMock.Setup (x => x.ExecuteQuery<Application> (predicate))
.Returns (() => new List<Application> { new Application (){
              Id = id,
              Title = "Application Title",
              SubTitle = "Application SubTitle",
              EmailOwner = "n****[email protected]",
              Copyright = "&copy; Copyright *** 2013.",
              IconSource = "2.png",
              IsTest = true,
              TestUid = TestUid,
              CreatedAt = DateTime.MinValue,
              UpdatedAt = DateTime.MinValue 
            }
          });

What am I doing wrong?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mayorovp, 2014-11-01
@nodir

The problem is:
x.ExecuteQuery (predicate)
Something tells me that Mock can't compare two predicates - that's null. Use It.IsAny instead of predicate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question