Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NerdDinner.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using NerdDinner.Models;
using Moq;
 
namespace NerdDinner.Tests.Controllers
{
    /// <summary>
    /// Summary description for DinnerControllerTest
    /// </summary>
    [TestClass]
    public class DinnerControllerTest
    {
        List<dinner> CreateTestDinner()
        {
            List<dinner> dinners = new List<dinner>();
            for (int i = 0; i < 101; i++)
            {
                Dinner sampleDinner = new Dinner()
                {
                    DinnerID = i,
                    Title = "New Title",
                    EventDate = DateTime.Now,
                    Description = "Something",
                    HostedBy = "SomeUser",
                    ContactPhone = "425-703-1272",//the 1st digit must be between [2-9]
                    Address = "Somewhere",
                    Country = "India",
                    Latitude = 93,
                    Longitude = -92
                };
                dinners.Add(sampleDinner);
            }
            return dinners; 
        }
 
        DinnerController CreateDinnerController()
        {
            var repository = new NerdDinner.Tests.Fake.FakeDinnerRepository(CreateTestDinner());
            return new DinnerController(repository);
        }
 
        DinnerController CreateDinnerControllerAs(string userName)
        {
            var mock = new Mock<controllercontext>();
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName);
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);
            var controller = CreateDinnerController();
            controller.ControllerContext = mock.Object;
            return controller;
        }
 

        [TestMethod]
        public void EditAction_Should_Redirect_When_Update_Successful()
        {
 
            // Arrange
            var controller = CreateDinnerControllerAs("SomeUser");
            int id = 1;
 
            FormCollection formValues = new FormCollection() {
                { "Dinner.Title", "Another value" },
                { "Dinner.Description", "Another description" }
            };
 
            controller.ValueProvider = formValues.ToValueProvider();
 
            // Act
            var result = controller.Edit(id, formValues) as RedirectToRouteResult;
 
            // Assert
            Assert.AreEqual("Details", result.RouteValues["Action"]);
            Assert.AreEqual(id, result.RouteValues["id"]);
        }
 

The above code snippet is a testcase for updating a record in the NerdDinner example using ASP.NET MVC.
The ValueProvider in the above code, as per during debuggin, gives an exception. Because of that the test case fails.
Posted 28-Jan-13 1:40am
Edited 28-Jan-13 1:44am
(no name)51.5K
v3
Comments
ryanb31 at 1-Feb-13 16:37pm
   
What's the exception?
sarahnimy at 3-Feb-13 22:54pm
   
a null exception

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100