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 8005309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:11:32+00:00 2026-06-04T17:11:32+00:00

I am Using Moq package for testing a Controller. HomeController.cs public class HomeController :

  • 0

I am Using Moq package for testing a Controller.
HomeController.cs

public class HomeController : Controller
{  readonly IPermitRepository _repository;
    public HomeController(IPermitRepository rep)
    {            this._repository = rep;        }

    public ViewResult Index()
    {
        ViewBag.Message = "Hello World";
        PermitModel model = _repository.GetPermitDetails();
        return View(model);
    }
}

In HomeControllerTest.cs

 [TestClass]
 Public class HomeControllerTest
{
    [TestMethod]
    public void Index()
    {       
        var messagingService = new Mock<IPermitRepository>();
        var controller = new HomeController(messagingService.Object);

        var result = controller.Index() as ViewResult;
       Assert.IsInstanceOfType(result.Model, typeof(PermitModel));
    }
}

But its giving error.
Assert.IsInstanceOfType failed. Expected type:. Actual type:<(null)>.

Can some one provide solution and also some inf about Moq package in MVC3.
Thanks in advance

  • 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. Editorial Team
    Editorial Team
    2026-06-04T17:11:33+00:00Added an answer on June 4, 2026 at 5:11 pm

    Moq returns null by default for each non void method call.

    So when in your controller you call _repository.GetPermitDetails(); it return null that’s why your test fails.

    You need to call Setup on your method to return something:

    var messagingService = new Mock<IPermitRepository>();
    messagingService.Setup(m => m.GetPermitDetails()).Returns(new PermitModel());
    var controller = new HomeController(messagingService.Object);
    

    You can find more info in the Moq quickstart on how to customize the mock behaviour.

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

Sidebar

Related Questions

I have a controller in C# using the ASP.Net MVC framework public class HomeController:Controller{
With the following code (using Moq 4.0.10501.6): HomeController controller = new HomeController(); ActionResult result
I'm doing some unit testing, and mocking some properties using Moq . Now, this
I have some code in a test using Moq: public class Invoice { ...
I'm mocking the WebOperationContext class over wrapper for unit testing (using Moq). But I
I am using the Moq framework for unit testing and would like to be
[using Moq] I am trying to mock a concrete class and mock a virtual
I'm using Moq to help in testing my ASP.NET MVC2 application. Problem: ArgumentException was
Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
I am using Moq for unit testing, and I am trying to write my

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.