Click here to Skip to main content
12,952,792 members (36,263 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 0:40am
Updated 28-Jan-13 0:44am
_Amy57.3K
v3
Comments
ryanb31 1-Feb-13 16:37pm
   
What's the exception?
sarahnimy 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
Top Experts
Last 24hrsThis month
OriginalGriff 6,389
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920


Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2017
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