Question

Just embarking on using a test framework for writing unit tests and also the TDD approach. Not having any prior experience felt it would be good to go for XUnit although NUnit was the best alternative. 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.

Specifically the following: Testing list of entries for a view collection like 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?

What puts me off is the lack of documentation for XUnit and am wondering if NUnit is better option.........

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? Not sure what the actual difference is apart from when they get written!

Was it helpful?

Solution

I am a fan of mspec. See these questions

Helpful links :

MSpec installer

It runs on top of NUnit. There are also mvc extension methods for things like

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();
}

OTHER TIPS

I don't know about XUnit but in NUnit there is collections constraints look at this 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.

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".

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top