我目前正在尝试使用Microsoft Fakes Framework编写一些单元测试。对于特定的单元测试,我必须模拟Controller.User属性。因此,我为System.Web添加了一个假程序集,并创建了System.Fakes.StubHttpContextBase的新实例:

var stubHttpContext = new System.Web.Fakes.StubHttpContextBase();


在下一步中,我想设置User属性的返回值。我希望在我的StubHttpContextBase实例上找到一个“ UserGet”属性,但是没有运气:



真正令我困惑的是,MS Fakes能够为除“ User”属性之外的所有其他属性生成吸气剂。我在这里想念什么吗?

最佳答案

var child = new StubHttpContextBase();
var basecontext=  new ShimHttpContextBase(child)
    {
        UserGet= () =>
            new GenericPrincipal(new GenericIdentity("mhu"), new string[0])
    };

var conCntx = new ControllerContext();
conCntx.HttpContext = basecontext;
controllerUnderTest.ControllerContext = conCntx;

关于c# - 使用Microsoft伪造品模拟HttpContextBase-缺少UserGet属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14139089/

10-11 00:47
查看更多