确保视图存在 [英] Making sure a view exists

查看:132
本文介绍了确保视图存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在寻找到单元测试一个新的应用程序,我要创造。我已经得到了基本的测试将会很好地(测试的ActionResult类是pretty不错)。有一件事我想确认虽然是一个在的ViewPage我的解决方案存在。我不是100%肯定,我的测试是正确的,所以如果任何人有意见,请不要犹豫!

I'm currently looking into unit testing for a new application I have to create. I've got the basic testing going nicely (testing the ActionResult classes is pretty nice). One thing I do want to make sure though, is that a viewpage exists in my solution. I'm not 100% sure my test is correct, so if anyone had suggestions, please don't hesitate!

这是一个测试,我必须检查我的安全控制器上我的登录方法做正确的事:

This is a test I have to check that my login method on my security controller is doing the right thing:

[TestMethod]
public void Login()
{
    var authProvider = new Mock<IAuthenticationProvider>();
    var controller = new SecurityController(authProvider.Object);

    var result = controller.Login() as ViewResult;

    Assert.IsNotNull(result, "ActionResult should be of type ViewResult.");
    Assert.AreEqual(result.ViewName, "login", "Does not render login page.");
}

我测试的解释是:

My explanation of the test would be:


  • 调用的方法登陆的控制器上

  • 确认它的渲染视图(通过检查它返回一个对象的ViewResult)

  • 确认它的渲染右视图(通过检查视图名)

我想有第三种断言,看是否被实际呈现的观点存在。

What I would like to have is a third assert, to see if the view to be rendered actually exists.

我有一些次要的问题将是:

Some secondary questions I have would be:


  • 我应该分裂这个测试呢?

  • 我应该将其重命名(如,呃,LoginRendersCorrectView什么的)

谢谢!


请注意:我明确地试图避免检查文件系统。我那种希望的方式来使用视图引擎来确认实际存在的看法。

Note: I'm explicitly trying to avoid having to check the filesystem. I'm sort of hoping for a way to use the ViewEngine to confirm the view actually exists.

推荐答案


  • 没有,我不认为只要你应该分开测试了作为其主要只是三分之一断言,而不是非常多code。

  • No, I don't think you should split the test up as long as its just mainly a third assert and not very much more code.

    是的,我觉得更描述性的名称将是有益的。

    Yes, I think a more descriptive name would be helpful.

    既然你已经验证它是否具有正确的视图名称已,不只是成功地渲染视图验证它的存在?

    Since you've verified it has the correct view name already, wouldn't simply successfully rendering the view verify its existence?

    我认为它的伟大,你是在完整的测试覆盖工作,但在这里,我觉得如果你能你在哪里验证移动到部分有可能是更有效地利用你的时间,对进行实际的具体单位登录功能(如校验密码哈希值或其他)是否正常工作。

    I think that its great you are working on complete test coverage but here I feel like there might be more effective use of your time if you were able to move on to the part where you verify that the units that perform the actual specific login functions (such as verifying password hashes or whatever) are working correctly.

    这篇关于确保视图存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆