简体   繁体   中英

Moq - How to return a mocked object from a method?

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
        }

    }
}

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);
} 

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"
                       };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM