简体   繁体   English

ASP.NET MVC中的单元测试

[英]Unit Testing in ASP.NET MVC

Just embarking on using a test framework for writing unit tests and also the TDD approach. 只是开始使用测试框架来编写单元测试以及TDD方法。 Not having any prior experience felt it would be good to go for XUnit although NUnit was the best alternative. 没有任何以前的经验,尽管NUnit是最好的选择,但选择XUnit还是不错的。 Trying to transpose the MS Unit testing methods that I have been looking at in the MVC books I have, to XUnit equivalents and am already stumbling. 试图将我在MVC书籍中一直在研究的MS Unit测试方法转换为XUnit等效方法,并且已经绊脚石了。

Specifically the following: Testing list of entries for a view collection like Index: 特别是以下内容:测试视图集合(如Index)的条目列表:

CollectionAssert.AllItemsAreInstancesOfType((ICollection)result.ViewData.Model,typeof(MyObject));  (from MVC unleashed book)

How would you do this in XUnit or can't it be done like this? 您将如何在XUnit中做到这一点?

What puts me off is the lack of documentation for XUnit and am wondering if NUnit is better option......... 让我失望的是缺少XUnit文档,并且想知道NUnit是否是更好的选择.........

Also it appears that the testing code is almost its own language. 而且看来,测试代码几乎是它自己的语言。 Would it be fair to say that there is a common set of tests that can be run for all projects? 可以公平地说,可以对所有项目运行一组通用的测试吗?

Regards to TDD..I understand the concept but are the tests themselves the same as unit tests in what they contain and are testing? 关于TDD ..我了解概念,但是测试本身与包含在测试中的单元测试是否相同? Not sure what the actual difference is apart from when they get written! 不知道他们写的时候实际的区别是什么!

I am a fan of mspec . 我是mspec的粉丝。 See these questions 看到这些问题

Helpful links : 有用的网址 :

MSpec installer MSpec安装程序

It runs on top of NUnit . 它在NUnit之上运行。 There are also mvc extension methods for things like 还有诸如以下内容的mvc扩展方法

result.ShouldBeAView().and().ShouldHaveModelOfType<T>()

A controller test can look like this 控制器测试如下所示

[Subject(typeof(JobsController))]
public class when_viewing_imdex_page : specifications_for_jobs_controller
{
    static ActionResult result;

    Establish context = 
        () => result = controller.Index();

    It should_return_a_view_result = 
        () => result.ShouldBeAView();

    It should_return_a_view_with_formviewdata = 
        () => result.ShouldBeAView().And().ShouldHaveModelOfType<IList<Job>>();

    It should_contain_a_list_of_jobs = 
        () => result.Model<IList<Job>>().ShouldNotBeEmpty();
}

I don't know about XUnit but in NUnit there is collections constraints look at this NUnit 我不了解XUnit,但在NUnit中有收集约束,请查看此NUnit

for your example you could use this code 对于您的示例,您可以使用此代码

Assert.That((ICollection)result.ViewData.Model, Is.All.InstanceOf<MyObject>());

I think it would be unfair for me to comment on which Testing Framework you should use, having only used NUnit, but it's always been enough for me. 我认为对仅使用NUnit的应使用的测试框架发表评论对我来说是不公平的,但是对于我而言,这已经足够了。

Regarding your second and third points however; 关于第二点和第三点; the majority of tests a very similar, but that is the point of TDD, start from the base, and continue refactoring until you have "no more, and no less". 大多数测试非常相似,但这就是TDD的意义,它从基础开始,然后继续进行重构,直到“没有更多,也没有更少”。

Test Driven Development and Test After Development are both a form of Unit Testing; 测试驱动开发和开发后测试都是单元测试的一种形式。 but within TDD, the tests are DRIVING the development, ensuring that every line you write has a purpose and is fully tested. 但是在TDD中,测试正在驱动开发,确保您编写的每一行都有目的并经过充分测试。

There are disadvantages; 有缺点; sometimes, you have to write the code before testing (especially when starting out), so you can figure out how to test it, so it may be fair to say that your development will contain a bit of both types mentioned. 有时,您必须在测试之前(尤其是在开始时)编写代码,这样您才能弄清楚如何进行测试,因此可以很公平地说您的开发将同时提到两种类型。

TDD, and any form of automated testing is definitely worth the effort, if for nothing but the satisfaction you get from seeing hundreds of tests pass in your application on your final test run. TDD和任何形式的自动化测试绝对值得您付出努力,即使您看到最终测试运行中有数百个测试通过您的应用程序而获得的满足感,也无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM