我知道这是一个老问题,可能会在其他地方得到回答,但在我正在处理的一个项目中,我们可以使用 Linqpad 调试控制器操作并获取返回值。
简而言之,你需要告诉 Linqpad 返回一些东西:
var result = homeController.Index();
result.Dump();
您可能还需要模拟您的上下文并将 Visual Studio 附加为调试器。
完整的代码片段:
void Main()
{
using(var svc = new CrmOrganizationServiceContext(new CrmConnection("Xrm")))
{
DummyIdentity User = new DummyIdentity();
using (var context = new XrmDataContext(svc))
{
// Attach the Visual Studio debugger
System.Diagnostics.Debugger.Launch();
// Instantiate the Controller to be tested
var controller = new HomeController(svc);
// Instantiate the Context, this is needed for IPrincipal User
var controllerContext = new ControllerContext();
controllerContext.HttpContext = new DummyHttpContext();
controller.ControllerContext = controllerContext;
// Test the action
var result = controller.Index();
result.Dump();
}
}
}
// Below is the Mocking classes used to sub in Context, User, etc.
public class DummyHttpContext:HttpContextBase {
public override IPrincipal User {get {return new DummyPrincipal();}}
}
public class DummyPrincipal : IPrincipal
{
public bool IsInRole(string role) {return role == "User";}
public IIdentity Identity {get {return new DummyIdentity();}}
}
public class DummyIdentity : IIdentity
{
public string AuthenticationType { get {return "?";} }
public bool IsAuthenticated { get {return true;}}
public string Name { get {return "sampleuser@email.com";} }
}
系统会提示您选择调试器,选择构建应用的 Visual Studio 实例。
我们有一个针对 MVC-CRM 的特定设置,所以这可能不适用于所有人,但希望这会帮助其他人。