本文介绍了嘲讽一个cotroller背景ASP 4.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不模拟控制器上下文,以便测试被授权的一些​​方法。现在的问题是,虽然我尝试的中这是行不通的,因为当我键入:

  VAR模拟=新的模拟< ControllerContext>( ); 



Visual Studio的下划线 ControllerContext ,并说





这是我已经包含在单元测试类usings:使用Microsoft.VisualStudio.TestTools.UnitTesting
;

 使用系统; 
使用的System.Web;
使用System.Collections.Generic;
使用起订量;使用System.Web.Http.Controllers
;使用Data.Contracts
;
使用System.Linq的;使用Web.Api.Controllers
;



任何想法如何,包括 ControllerContext 在MSDN文档他们说,这是System.Web程序的一部分,但它似乎并不奏效。它也将是巨大的,如果有另一种方式来嘲笑不使用此背景下。



我固定由intalling了MVC包的参考。但现在当我尝试这个收到以下错误:

  VAR模拟=新的模拟< ControllerContext>(); 
mock.SetupGet(X => x.HttpContext.User.Identity.Name).Returns(指someUser);
mock.SetupGet(X => x.HttpContext.Request.IsAuthenticated).Returns(真);
controller.ControllerContext = mock.Object;



不能转换 System.Web.Mvc.ControllerContext System.Web.Http.Controllers.ControllerContext

 > controller.ControllerContext = mock.Objectt; 


解决方案

的 t是系统的一部分.Web.Mvc System.Web.Mvc.dll程序。只需添加参考吧。


I have to mock the controller context in order to test some methods that are authorized. The problem is that although I try this it does not work because when I type:

var mock = new Mock<ControllerContext>();

Visual Studio underlines ControllerContext and says that

These are the usings i have included in the unit test class: using Microsoft.VisualStudio.TestTools.UnitTesting;

    using System;
    using System.Web;
    using System.Collections.Generic;
    using Moq;
    using System.Web.Http.Controllers;
    using Data.Contracts;
    using System.Linq;
    using Web.Api.Controllers;

Any ideas how to include ControllerContext in the MSDN documentation they say that it is part of System.Web but it does not seem to be working. Also it will be great if there is another way to mock the context without using this.

I fixed the reference by intalling the Mvc package. But now receive the following error when I try this:

var mock = new Mock<ControllerContext>();
            mock.SetupGet(x => x.HttpContext.User.Identity.Name).Returns("SOMEUSER");
            mock.SetupGet(x => x.HttpContext.Request.IsAuthenticated).Returns(true);
            controller.ControllerContext = mock.Object; 

Cannot convert System.Web.Mvc.ControllerContext to System.Web.Http.Controllers.ControllerContext

> controller.ControllerContext = mock.Objectt;
解决方案

ControllerContext is a part of System.Web.Mvc in System.Web.Mvc.dll. Just add reference to it

这篇关于嘲讽一个cotroller背景ASP 4.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:34