【问题标题】:Unit Test Friendly way of using membership details inside the controller在控制器内使用成员详细信息的单元测试友好方式
【发布时间】:2011-10-03 07:35:19
【问题描述】:

这是我的 ASP.NET MVC 3 应用程序中的一个控制器中的一段代码:

_destinationdetailRepository.Add(new DestinationDetail { 

    DestinationID = destination.DestinationID,
    CreatedOn = DateTime.Now,
    CreatedBy = User.Identity.Name
});

这里重要的是 CreatedBy 属性值,即User.Identity.Name。它工作得很好,我也在我的应用程序的其他部分使用它。但是,我想这不是一种单元测试友好的做事方式。

那么,在控制器内部使用会员数据的方式是什么,以便在我对我的应用进行单元测试时感到高兴。

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 unit-testing asp.net-membership


    【解决方案1】:

    但是,我想这不是一种友好的单元测试方式。

    不,它对单元测试很友好,而且是正确的代码。 User 属性是一个IPrincipal 接口,可以在单元测试中模拟。

    【讨论】:

    • 太酷了。我会深入研究的。已经找到一堆东西了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 2011-11-29
    • 2020-09-28
    • 2011-02-16
    • 1970-01-01
    • 2013-07-02
    • 2017-07-15
    相关资源
    最近更新 更多