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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:38:18+00:00 2026-05-12T14:38:18+00:00

var mockedService = new Mock<IService>(); mockedService.Setup(x => x.InterfaceMethod(args)).Returns(value); _Service = mockedService.Object; MyController controller =new

  • 0
var mockedService = new Mock<IService>();
mockedService.Setup(x => x.InterfaceMethod(args)).Returns(value);
_Service = mockedService.Object;
MyController controller =new MyController(_Service);

var result = (ViewResult)controller.Foo();

Now this Foo() Method contains the Following API Call

HttpContext.GetGlobalResourceObject(...,..);

But the HTTPContext is null, as i’m just unit testing , i have not Authenticated. and hence not populated the HTTPContext Object.

I cant set expectations on this class because the HTTPContext is sealed.

I’ve been searching in the net , but there were only samples to fake the HTTPContextBase

Now how do i get my Unit Test to Pass. Any Suggestions.?

thanks ,

vijay

  • 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-05-12T14:38:18+00:00Added an answer on May 12, 2026 at 2:38 pm

    The problem is you are not setting up an expectation for the “Centres” method. When you mock an object all implementations are replaced with a Noop until you specify otherwise using an Expectation.

    I’m guessing you mocked GetValueFromResource rather than Centres because Centres calls GetValueFromResource. If this is the case, mocking GetValueFromResource is likely a superfluous fake and you don’t need it.

    Also, as has been pointed out on several other Moq posts, you’ll need to make sure that you are not mocking what you are testing. If you are actually trying to test MyController, you’d want to mock any dependencies it has, but not MyController itself. This will cause you no end of confusion.

    Edit

    Based on this feedback it seems you have learned that your true problem is that you need to mock HTTPContext. Mocking HTTPContext is nontrivial. There are a few tools that can do it natively, like Type Mock, but the problem is that ambient context is not testable.

    What you should be doing is requesting dependencies that give you the information you need while abstracting away their implementation. For example, in your case it seems like you are using HTTPContext to access resources. If you need something like this a good interface you could use would be:

    public interface IResourceStore
    {
         object GetGlobalResource(string  classKey, string resourceKey);
    }
    

    Your default implementation would use HTTPContext, but now you have an interface you can mock and have your MyController class use, rather than HTTPContext directly.

    Mock<IResourceStore> resourceStore = new Mock<IResourceStore>();
    //setup resourceStore expectations here
    
    MyController testingTarget = new MyController(_Service, resourceStore.Object);
    //target assertions here
    

    Hope this helps.

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

Sidebar

Related Questions

var ref1 = new Firebase(http://gamma.firebase.com/myuser/123,456); ref1.set(123,456); var on1 = ref1.on(value, function(snapshot) { console.log(snapshot.val()); });
var search= document.getElementById('appMenu').value document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch', params: ['query': search])}' The element appMenu
var queueitem = context.CrawlerQueues. Select(cq => new{cq.Guid,cq.Result}). SingleOrDefault(cq => cq.Guid == guid); Is above
var chart; var d = new Array(); function click() { d = document.getElementById('graph:hi').value; alert(d);
var longitudeArray = new Array(<?php $result = count($longitudeArray); if ($result > 1){ echo implode(',',
var idParam = new SQLiteParameter(@idParam, SqlDbType.Text) { Value = insertData.ID }; var userIdParam =
var $content = $('#SomeDivContainingTwoImages'); $content.children().each(function(i){ $(this).showImage = showImageStatic; $(this).showImage(); }); returns Uncaught TypeError: Object
var Foo = Foo || {}; Foo.Controller = (function ($) { var $page =
var t={a:1,b:2} is this an Object with properties (a,b) or it is a Javascript
var arr = [{ key: key1, value: z }, { key: key2, value: u

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.