简体   繁体   中英

Unit Test Mocking an IHttpActionResult GetId method

I am creating some unit tests for a controller I have however I ran into a problem.

Basically I have the following:-

The Controller Method :-

[ResponseType(typeof(Attrib))]
public IHttpActionResult GetAttrib(int id)
{
    var attrib = _attribsRepository.GetAttrib(id);
    if (attrib == null)
    {
        return NotFound();
    }

    return Ok(attrib);
}

Its a simple Web API 2.0 method.

Then I have the Repository :-

public Attrib GetAttrib(int id)
{
    return DbSet.FirstOrDefault(x=>x.Id == id);
}

And finally the Test Method:-

public class AttribsControllerTests
{
    public Mock<IAttribsRepository> _attribsRepositoryMock;
    public List<Attrib> AttribList;
    public AttribsController AttribsController;
    [SetUp]
    public void Init()
    {
        _attribsRepositoryMock = new Mock<IAttribsRepository>();
        AttribList = new List<Attrib>
        {
            new Attrib()
            {
                Id = 1,
                AttributeId = "Cro",
                AttributeName = "Crossing",
                AttributeType = "Tech",
                AttributeValue = 1
            },
            new Attrib()
            {
                Id = 2,
                AttributeId = "Dri",
                AttributeName = "Dribbling",
                AttributeType = "Tech",
                AttributeValue = 2
            },
            new Attrib()
            {
                Id = 3,
                AttributeId = "Fin",
                AttributeName = "Finishing",
                AttributeType = "Tech",
                AttributeValue = 3
            }
        };
    }

    [Test]
    public void Get_Check_That_Id1_Returns_Crossing()
    {
        //Arrange
        _attribsRepositoryMock.Setup(t => t.GetStaticAttribs()).Returns(AttribList.AsQueryable());

        //Act
        var attribsController = new AttribsController(_attribsRepositoryMock.Object);

        var result = attribsController.GetAttrib(1) as OkNegotiatedContentResult<Attrib>;

        //Assert
        Assert.IsNotNull(result);
        Assert.AreEqual(AttribList[0].AttributeName, "Cor");
    }
}

For some reason, the result is always null, so its not hitting the controller correctly.

Any ideas why this could happen? When debugging, the correct Mock Repository is hitting the controller, and it should have the 3 entries in it.

Any help will be very much appreciated.

You setup GetStaticAttribs but it is used nowhere in the example you showed. You were suppose to setup IAttribsRepository.GetAttrib

Based on your example

[Test]
public void Get_Check_That_Id1_Returns_Crossing() {
    //Arrange
    var id = 1;
    _attribsRepositoryMock.Setup(t => t.GetAttrib(id)).Returns(AttribList[0]);
    var attribsController = new AttribsController(_attribsRepositoryMock.Object);

    //Act
    var result = attribsController.GetAttrib(id) as OkNegotiatedContentResult<Attrib>;

    //Assert
    Assert.IsNotNull(result);
    Assert.IsNotNull(result.Content);
    Assert.AreEqual(result.Content.AttributeName, "Crossing");
}

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