1

这是我的一个单元测试类的示例(简化为基础)。在控制器中,当调用 Index() 操作方法时,对 GetByID(1234) 的调用总是会产生一个新的 Ticket 对象实例。该对象存在,但它的所有属性都是空的,即使我已经在我的假对象中设置了它们。关于为什么的任何想法?

我正在使用起订量。

单元测试

    [TestClass]
    public class TicketControllerTests : ControllerTestBase
    {
        protected Mock<ITicketRepository> MockTicketRepository = new Mock<ITicketRepository>();

        [TestMethod]
        public void IndexActionModelIsTypeOfTicketModel()
        {
            //ARRANGE
            Mock<HttpContextBase> context = FakeHttpContext();
            context.Setup(ctx => ctx.Session[SessionKeys.TokenData.ToString()]).Returns(Constants.TOKENDATA_SUBMITTER);

            MockTicketRepository.Setup(x => x.GetById(It.IsAny<int>())).Returns(Constants.CLIENT_TICKET);

            //ACT
            var result = GetController(context.Object).Index(Constants.TICKET_ID);
            var model = ((ViewResult)result).Model;

            //ASSERT
            Assert.IsInstanceOfType(model, typeof(TicketModel), "ViewModel should have been an instance of TicketModel.");
        }

        private TicketController GetController(HttpContextBase context)
        {
            var controller = new TicketController(MockTicketRepository.Object);
            controller.ControllerContext = GetControllerContext(context, controller);
            return controller;
        }
    }

常量.CLIENT_TICKET

public static Ticket CLIENT_TICKET
        {
            get
            {
                var ticket = new Ticket
                {
                    CategoryID = 1,
                    CreatedByUserId = 4
                };

                ticket.Clients.Add(new Client { ShortName = "Test Client 1"});
                ticket.Clients.Add(new Client { ShortName = "Test Client 2" });

                ticket.User = new User {FirstName = "First", LastName = "Last"};

                return ticket;
            }
        }

控制器

private readonly ITicketRepository _ticketRepository;

public TicketController(ITicketRepository ticketRepository)
{
    _ticketRepository = ticketRepository;
}

public ActionResult Index(int id)
{
    var ticket = _ticketRepository.GetById(id);

    // etc...
}
4

1 回答 1

1

你能显示正在测试的控制器代码吗?这可能与您如何设置模拟上下文有关,但如果不查看控制器代码就很难判断。

此外,如果您MockBehavior.Strict在创建模拟时添加,如果调用没有相应的期望,它将被炸毁:

protected Mock<ITicketRepository> MockTicketRepository = new Mock<ITicketRepository>(MockBehavior.Strict);

更新我试图剥离一切,以便测试尽可能简单地尝试和隔离问题。这是我想出的:

[TestClass]
public class TicketControllerTests : ControllerTestBase
{
    protected Mock<ITicketRepository> MockTicketRepository;

    [TestMethod]
    public void IndexActionModelIsTypeOfTicketModel()
    {
        //ARRANGE
        MockTicketRepository = new Mock<ITicketRepository>(MockBehavior.Strict);
        MockTicketRepository.Setup(x => x.GetById(Constants.TICKET_ID)).Returns(Constants.CLIENT_TICKET);
        var controller = new TicketController(MockTicketRepository.Object);

        //ACT - try to keep ACT as lean as possible, ideally just the method call you're testing
        var result = controller.Index(Constants.TICKET_ID);

        //ASSERT
        var model = ((ViewResult)result).ViewData.Model;
        Assert.That(model, Is.InstanceOfType<TicketModel>(), "ViewModel should have been an instance of TicketModel.")
    }
}
于 2013-02-04T18:08:25.797 回答