views:

32

answers:

0

Hi All,

I am very new to Testing and MVC.NET, and I have some code in the Create of the ArticleController which I wish to test, however I need some help because at the moment I am kind of stuck.

My Code is as follows :-

        if(ModelState.IsValid)
        {
            try
            {
                if (!User.IsInRole("Administrator"))
                {
                    return View("InvalidOwner");
                }

                CategoryRepository categoryRep = new CategoryRepository();
                this.categoryRep = categoryRep;

                var categoryID = Request["CategoryID"];
                Category category = new Category();
                //if its a new category, then the category will be null, so insert a new one
                bool CategoryExists = true;
                category = categoryRep.GetCategoryByID(Convert.ToInt32(categoryID));
                if (category.CategoryTitle == "")
                {
                    category.CategoryTitle = Request["CategoryTitle"];
                    CategoryExists = false;
                }

                var AddedDate = Request["AddedDate"];
                var ReleaseDate = Request["ReleaseDate"];
                var ExpiredDate = Request["ExpiredDate"];
                var Body = Request["Body"];

                article.AddedDate = Convert.ToDateTime(AddedDate);
                article.ReleaseDate = Convert.ToDateTime(ReleaseDate);
                article.ExpiredDate = Convert.ToDateTime(ExpiredDate);
                article.Body = Body;

                if (CategoryExists)
                    articleRep.Insert(article);
                else
                    articleRep.Insert(article, category);


                return RedirectToAction("Details", new { id = article.ArticleID });

            }
            catch (Exception exc)
            {
                string errMess = exc.Message;
                ModelState.AddRuleViolations(article.GetRuleViolations());
                throw;
            }

Up until now, I have the following in my test:-

    [Test, DataRollBack]
    public void Create_CanInsertNewArticleView_IsNotNull()
    {
        // Arrange
        var controller = new ArticleController();

        var mockRepo = new MockRepository();

        var userIdentity = mockRepo.StrictMock<System.Security.Principal.IIdentity>();

        string[] roles = {"Administrator"};
        var user = new GenericPrincipal(userIdentity, roles);
        var httpCtx = MockRepository.GenerateStub<HttpContextBase>();
        httpCtx.User = user;
        httpCtx.User.IsInRole("Administrator");

        var controllerCtx = new ControllerContext();
        controllerCtx.HttpContext = httpCtx;

        controller.ControllerContext = controllerCtx;

        // Act
        Article fakeArticle = FakeObjects.ReturnFakeArticle();

        var result = controller.Create(fakeArticle) as ViewResult;

        // Assert
        Assert.IsNotNull(result);
    }

Can anyone suggest me what to do next, since, I need to pass some parameters to the Controller, such as :- var categoryID = Request["CategoryID"]; var AddedDate = Request["AddedDate"]; var ReleaseDate = Request["ReleaseDate"]; var ExpiredDate = Request["ExpiredDate"]; var Body = Request["Body"];

Thanks for your help and time

Johann