3

I am new to Moq and want to use it not just in unit tests where I seem to mostly get it but in code.

Given this entity:

 namespace TestBed.Domain.Entities
 {
    public class Person
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string PhoneNumber { get; set; }
        public string JobTitle { get; set; }
    }
 }

and this abstract Repository:

using TestBed.Domain.Entities;

namespace TestBed.Domain.Abstract
{
    public interface IPersonRepository
    {
        Person GetPerson();
    }
}

I want to use Moq to populate a dummy person (!) and pass that populated "object" out of the Repository method. How do I do this?

using TestBed.Domain.Abstract;
using TestBed.Domain.Entities;
using Moq;

namespace TestBed.Domain.Concrete
{
    public class MockPersonReqpository
    {
        Person GetPerson()
        {
            Mock<IPersonRepository> mock = new Mock<IPersonRepository>();
            mock.Setup(m => m.GetPerson()).Returns(new Person()
                                                       {
                                                           FirstName = "Joe",
                                                           LastName = "Smith",
                                                           PhoneNumber = "555-555-5555"
                                                       });
            return mock.Object // NO
        }

    }
}

2 Answers 2

11

From your comment on @Daniel's answer, it seems like you just need to mock the repository itself. You would still want to return a proper Person object, you just don't care how the repository is actually retrieving that person for the purposes of your test.

I also don't understand your comment about using Moq in other places besides unit tests. The whole point of Moq is so that you can fake an actual object for testing purposes (probably better said than that, but that's the gist).

Since I don't know what you're trying to test exactly, I'll give a trivial example:

[TestMethod]
public void WhenValidRequest_ThenReturnSuccess()
{
    Mock<IPersonRepository> personRepository = new Mock<IPersonRepository>();
    personRepository.Setup(r => r.GetPerson()).Returns(
        new Person() 
        { 
            FirstName = "Joe",
            LastName = "Smith"
            /*...*/
        });

    Foo objectUnderTest = new Foo(personRepository.Object);

    bool result = objectUnderTest.MakeRequest(); 
    // Call method using the person repository that you want to test.
    // You don't actually care how the repository works, you just want to return a success 
    // boolean when a person exists for that request.

    Assert.IsTrue(result);
} 
1
  • Thanks for this. I was under the impression that var mock = new Mock<T>() produced the same resulting type as var mock = new Mock<T>.Setup(). But it doesn't. So, I shouldn't have tried try to construct and manipulate in the same call. Feb 3, 2016 at 13:09
4

mock.Object returns a mock of type IPersonRepository. The return type for the GetPerson() method expects an object of type Person.

The other thing is that you are not creating a mock for Person, so I don't see the point of this code. I am not sure what you mean by dummy but you could just as well do this:

Person GetPerson()
{
    return new Person()
                       {
                           FirstName = "Joe",
                           LastName = "Smith",
                           PhoneNumber = "555-555-5555"
                       };
}
2
  • I know I could that but I want to create a mocked IPersonRepository and then a database EntityFramework one EFProductDatabase where the GetPerson returns real data. Does that make any sense? Do I need to create a mock for Person or just the Repository? How could I do what I want with Moq? Thanks! Jul 13, 2011 at 20:01
  • As I see it Moq is an aid for testing, it mocks away all the dependencies so that you can test the logic of the code under test. You can do everything that Moq does manually (mocks and stubs), Moq just makes it easier. A mock is a way to assert if the object under test has interacted as expected with the mock. In your case, you just want to create a Person with some fake data if I understand you correctly? You don't need Moq for that. Just create a FakePersonRepository that returns a Person with some fake data.
    – Daniel Lee
    Jul 13, 2011 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.