Im new to mvc 3 and I'm doing unit tests. I'm testing a search action method. This method returns an action method that contains a generic list of some type. How do I test if the model data returned is of that specified type? Please help.
Asked
Active
Viewed 327 times
2 Answers
0
In your test method you do a type assertion after you have obtained the search results in a variable. Here is an assert for NUnit:
var searchResults = SearcherUnderTest.Search("TestKeyword");
Assert.IsInstanceOfType( Type expected, object searchResults );

Maxim V. Pavlov
- 10,303
- 17
- 74
- 174
0
Do you mean that you want to test the type of T in a List? If so then look at this question: How to get the type of T from a member of a generic class or method?
Or do you need help with writing a unit test for an action? Then: How to unit test an ActionResult that returns a ContentResult?
Testing with Nunit, it usually looks like this when testing search results:
[Test]
public void Search_ShouldReturnAListOfOrders()
{
var result = _controller.Search("searchParameter") as MyViewModel ;
Assert.That(result, Is.Not.Null);
Assert.That(result.SearchResults, Is.Not.Null);
Assert.That(result.SearchResults.Count, Is.GreaterThan(0));
}

Community
- 1
- 1

Daniel Lee
- 7,709
- 2
- 48
- 57
-
Lets say i have a class screenmodel and screensmodel. screens model contains List
. my action method is search(string types) and it should return the screensmodel that has the screenmodel list. in my unit test i want to check if the action methos returns the "List – CodeNoob Mar 02 '12 at 09:34" list -
Are you using Stubs/Mocks? Or is this testing the database as well? Anyway I would say you just need to test that the list is not null and that count > 0. – Daniel Lee Mar 02 '12 at 09:39
-
Thanks what. I used Assert.IsTue(returnModel is screensmodel) since the list is inside the screens model class – CodeNoob Mar 02 '12 at 09:53
-
Yeah, that's pretty much the same thing as using the "as" keyword. – Daniel Lee Mar 02 '12 at 10:11