English 中文(简体)
正确的单位测试方言。 发动机。 FindView?
原标题:What is the correct way of Unit testing ViewEngines.Engines.FindView?

我最近对我的微薄应用做了一些调整,并认识到,已经恢复了一些静态看法。 我决定建立一个控制者,如果存在固定观点,如果存在404个错误,如果这种观点存在的话,则放弃这种错误。

public ActionResult Index(string name)
{
    ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);

    if (result.View == null)
        ThrowNotFound("Page does not exists.");

    return View(name);
}

我的问题是,哪一种是正确的单位检验方法? 我尝试了以下法典,但我发现的错误是:“航道必须包含一个称为控制器的物品,具有非防伪价值”。

[Theory]
[InlineData("ContactUs")]
public void Index_should_return_view_if_view_exists(string name)
{
    controller = new ContentController();
    httpContext = controller.MockHttpContext("/", "~/Content/Index", "GET"); ;

    var result = (ViewResult)controller.Index(name);

    Assert.NotNull(result.View);
}

我的用意是,这个单位要试着去看,看真的。 接着,我开始问,我是否应当将“观点”与“FetupGet for FindView”混为一谈,并创设两个测试,在这两个试验中,如果意见无效,就会推翻未发现的例外。

如何正确测试这一功能? 任何点人、样本代码或博客员额都是有益的。

增 编

最佳回答

你们应当创建一种 mo形的视角引擎,并把它放在收集工作中:

[Theory]
[InlineData("ContactUs")]
public void Index_should_return_view_if_view_exists(string name)
{
    var mockViewEngine = MockRepository.GenerateStub<IViewEngine>();
    // Depending on what result you expect you could set the searched locations
    // and the view if you want it to be found
    var result = new ViewEngineResult(new [] { "location1", "location2" });
    // Stub the FindView method
    mockViewEngine
        .Stub(x => x.FindView(null, null, null, false))
        .IgnoreArguments()
        .Return(result);
    // Use the mocked view engine instead of WebForms
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(mockViewEngine);

    controller = new ContentController();

    var actual = (ViewResult)controller.Index(name);

    Assert.NotNull(actual.View);
}
问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签