0

I want to test the given method using Moq. How do I make my controller use moq objects? New objects are being created inside the controller method. One of the class is datacontext and other one is also a concrete class. Facing trouble in creating a mock for these classes alslo.

//My Controller class action method public ActionResult GetRelatedCourses(int stuId) { if (Request.Cookies["RememberUsername"] != null) { if (System.Web.HttpContext.Current.Cache.Get(userName) == null) { CacheUtil.UpdateCache(userName); } Dictionary<string, int> sectionsCourses = (Dictionary<string, int>)System.Web.HttpContext.Current.Cache.Get(userName); if (sectionsForums.ContainsValue(stuId)) { var contextWrapper = new DataContextWrapper(dataContext); var courseRepository = new CourseRepository(contextWrapper); bool Student_Exists = StudentExists(stuId); if (Student_Exists) { IEnumerable<Course> course = courseRepository.GetRelatedCourses(stuId); var UserProfiles = contextWrapper.dataContext.usp_GetUserProfilesByStudentID(stuId).ToList(); ViewBag.UserProfiles = UserProfiles; if (course.Count() == 0) { ViewBag.ErrorMessage = "No courses available."; } return View(courses); } return View("NoStudentExists"); } } return View("NoAccess"); } 
1
  • @Patrick Quirk : Any suggestions? Commented Oct 13, 2014 at 23:53

1 Answer 1

1

You need to use Dependency Injection and a good IoC like Ninject. Instantiating data objects inside your methods is a no go if you want unit tests.

Sign up to request clarification or add additional context in comments.

1 Comment

The thing is that the code is already out there. And I have to write the tests. Is there no way other than going and changing the code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.