Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 118773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:32:52+00:00 2026-05-11T03:32:52+00:00

I am in the process of writing some unit tests for my controllers in

  • 0

I am in the process of writing some unit tests for my controllers in RC1. Here is the controller’s public signature I’m testing:

        [AcceptVerbs(HttpVerbs.Post)]     public ActionResult AcceptColleague()     { 

The implementation inside the AcceptColleague uses the TryUpdateModel(colleague) method of populating the Colleague object from the form fields. However, I’m running into a ‘Object reference not set to an instance of an object’ error on the TryUpdateModel line while trying to unit test the method.

Here is my unit test code:

            // definition         HomeController controller = new HomeController();         IColleagueRepository fakeColleagueRepo = MockRepository.GenerateMock<IColleagueRepository>();         Colleague requestedColleauge = new Colleague();         EmployeeInfo currentUser = new EmployeeInfo();                     HttpContextBase fakeHttpContext = MockRepository.GenerateMock<HttpContextBase>();         HttpRequestBase fakeHttpRequest = MockRepository.GenerateMock<HttpRequestBase>();         ControllerContext fakeContext = MockRepository.GenerateMock<ControllerContext>(fakeHttpContext, new RouteData(), controller);         NameValueCollection fakeForm = new NameValueCollection();          // expectations         fakeColleagueRepo.Expect(c => c.Read(1234)).Return(requestedColleauge);         fakeColleagueRepo.Expect(c => c.Update(requestedColleauge));         fakeColleagueRepo.Expect(c => c.Add(new Colleague())).IgnoreArguments().Constraints(Is.NotNull());         fakeContext.Expect(cx => cx.HttpContext).Return(fakeHttpContext);         fakeHttpContext.Expect(hcx => hcx.Request).Return(fakeHttpRequest);         fakeHttpRequest.Expect(hr => hr.Form).Return(fakeForm);          // setup         controller.ColleagueRepository = fakeColleagueRepo;         controller.ControllerContext = fakeContext;         requestedColleauge.TargetEmployeeInfoId = 123456;         requestedColleauge.GeneratedEmployeeInfoId = 654321;         currentUser.EmployeeInfoId = 123456;         fakeForm.Add('ColleagueId', '22222');          // action         RedirectToRouteResult result = controller.AcceptColleague() as RedirectToRouteResult;          // validation         Assert.IsNotNull(result, 'AcceptColleague() did not return RedirectToRouteResult'); 

Am I missing something on the mocking or should I be using a different public signature like AcceptColleague(Colleague colleague) and then testing the ModelState.IsValid property?

If so, how don’t see a way I can mock the read only ModelState property off of the controller?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:32:52+00:00Added an answer on May 11, 2026 at 3:32 am

    TryUpdateModel and ModelState both require zero mocks in RC 1. The only thing that you have to supply is a ValueProvider. For that, you can use FormCollection.ToValueProvider().

    You’ll still need to mock your repository, but there is nothing that you need to mock in the framework for this functionality. Try this:

        FormCollection fakeForm = new FormCollection();     fakeForm.Add('ColleagueId', '22222');     controller.ValueProvider = fakeForm.ToValueProvider();      // action 

    Note: No mocked HttpContext needed, unless your code requires it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've done unit testing some, and have experience writing tests, but I have
I am in the process of writing some unit tests. In particular I want
I'm in the process of writing unit tests for a JEE5 web service. The
I'm thinking, while writing some unit tests for an application to build intuitive methods
I am in the process of writing some validation code based on these assumptions:
We are currently going through the long process of writing some coding standards for
Let's say you had an external process writing files to some directory, and you
I am trying to fully understand the process pro writing code in some language
I'm in the process of writing a jQuery plugin, and am getting into some
I would like seek some guidance in writing a process profiler which runs in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.