Aug022009
Published by volkanuzun at 6:50 PM under
public ActionResult List() { return View("List",postRepository.Posts.Where(p=>p.IsApproved==true)); }
[Test] public void List_Should_Only_Pass_Approved_Posts_To_View() { //ARRANGE IPostRepository repository = new FakePostRepository(); PostsController controller = new PostsController(repository); //ACT var view = (ViewResult) controller.List(); var posts = view.ViewData.Model as IQueryable<Post>; //ASSERT posts.ToList().ForEach(c => Assert.True(c.IsApproved == true)); }
Let’s goto MSDN and checkout “List<T>.ForEach()”'; according to the definition; this function performs the specified action on each element of the List<T>; and the declaration of the function is:
public void ForEach( Action<T> action )
Basically it is taking Action as a parameter; and calling this for each element of the List. In case you are not familiar with this delegate; it is basically any function that takes a single parameter and does not return anything. Going back to my test function; I should be Asserting each element and checking if IsPublished==true or not. An Action<T> delegate could easily be c=>Assert.True(c.IsApproved==true)). Assert.True(bool condition) is a function which returns void; and takes only 1 parameter which a condition; so I can pass this to the ForEach function and test my assumption.
Tags:
E-mail | Permalink | Trackback | Post RSS 0 Responses
Volkan Uzun I work on IdM solutions and SharePoint lately :) E-mail me
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.